国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android 集成 google 登錄并獲取性別等隱私信息的實(shí)現(xiàn)代碼

瀏覽:67日期:2022-09-23 17:02:45

前言

公司做海外產(chǎn)品的,集成的是 google 賬號(hào)登錄,賬號(hào)信息、郵箱等這些不涉及隱私的按 google 的正常登錄流程可以輕松實(shí)現(xiàn) 。但是一旦需要獲取涉及隱私的信息就比較麻煩,文檔也不是十分清晰,非常難找,很多坑。

google 賬號(hào)登錄

官方鏈接:https://developers.google.com/identity/sign-in/android/starthttps://developers.google.com/identity/sign-in/android/sign-ingoogle 賬號(hào)登錄接入的坑:

申請(qǐng)的 client_id必須是 api console 后臺(tái) :https://console.cloud.google.com/apis 與 google play 后臺(tái)對(duì)應(yīng)的應(yīng)用關(guān)聯(lián)起來(lái)。 client_id 下的簽名信息和報(bào)名信息必須和測(cè)試時(shí)的 apk 的簽名信息和報(bào)名信息一致。 在 google play 下啟動(dòng) google 的二次簽名,則 api console 后臺(tái)的簽名信息是二次簽名后的信息。打包測(cè)試時(shí)使用上傳 到 Google play 后臺(tái)的 apk 的簽名證書(shū)即可。

google 登錄的流程在這個(gè)文檔寫的比較清楚了:https://developers.google.com/identity/sign-in/android/sign-in,這里大致說(shuō)一下,不貼代碼了

構(gòu)建需求請(qǐng)求的內(nèi)容:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestIdToken('your client_id') .build();// Build a GoogleSignInClient with the options specified by gso.mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

2.發(fā)起登錄請(qǐng)求,跳轉(zhuǎn) google 登錄頁(yè)面。

Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN);

獲取 Google 登錄返回

@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { // The Task returned from this call is always completed, no need to attach // a listener. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); handleSignInResult(task); }}

獲取 用戶 id token,傳到你自己的 服務(wù)端 做驗(yàn)證

private void handleSignInResult(Task<GoogleSignInAccount> completedTask) { try { GoogleSignInAccount account = completedTask.getResult(ApiException.class); // Signed in successfully, show authenticated UI. } catch (ApiException e) { // The ApiException status code indicates the detailed failure reason. // Please refer to the GoogleSignInStatusCodes class reference for more information. Log.w(TAG, 'signInResult:failed code=' + e.getStatusCode()); }}

切換賬號(hào)

/** * 重新獲取賬號(hào)列表 */ public void revokeAccess() { try { if (mGoogleSignInClient!=null && mActivity!=null){ mGoogleSignInClient.revokeAccess().addOnCompleteListener(mActivity, new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { Log.d(TAG, 'onComplete: '); } }); } } catch (Exception e){ e.printStackTrace(); } }

獲取公開(kāi)資料和需要特別授權(quán)的信息(性別、生日等)

1、在構(gòu)建請(qǐng)求是新增獲取 的公共資料信息 及 需要獲取的特殊信息

private static final String GENDER_SCOPE = 'https://www.googleapis.com/auth/user.gender.read';GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestIdToken('your client_id') .requestScopes(new Scope(GENDER_SCOPE)); .build();// Build a GoogleSignInClient with the options specified by gso.mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

需要請(qǐng)求的信息可在如下鏈接查找:https://developers.google.com/people/api/rest/v1/people/get

2、檢測(cè)是否有權(quán)限

GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(mActivity); Scope scope = new Scope(GENDER_SCOPE); if (Utils.isNeedRequest() && !GoogleSignIn.hasPermissions(lastSignedInAccount,scope)){ SGLog.d(TAG+' need requst permission...'); GoogleSignIn.requestPermissions(mActivity,RC_GET_TOKEN,lastSignedInAccount,scope); }

注意:這一步不需要也可以,有這一步會(huì)出現(xiàn)一個(gè) “再確認(rèn)” 的授權(quán)頁(yè)面,沒(méi)有也不影響獲取的信息。3、跳轉(zhuǎn)登錄頁(yè)面 (同以上 google 賬號(hào)登錄)4、獲取登錄信息 (同以上 Google賬號(hào)登錄)5、開(kāi)啟線程獲取 特殊信息

getProfileAsyncTask = new GetProfileAsyncTask(mActivity, new GpProfileInfoCallback() { @Override public void onGetProfileInfo(Person person) { SGLog.d(TAG+' onGetProfileInfo... '); getProfileInfo(person); } }); getProfileAsyncTask.execute(signInAccount);

異步任務(wù)

// Global instance of the HTTP transport private static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport(); // Global instance of the JSON factory private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); private static class GetProfileAsyncTask extends AsyncTask<GoogleSignInAccount, Void, Person> { // Retrieved from the sigin result of an authorized GoogleSignIn private WeakReference<Activity> mActivityRef; private GpProfileInfoCallback mProfileInfoCallback; public GetProfileAsyncTask(Activity activity,GpProfileInfoCallback callback) { mActivityRef = new WeakReference<>(activity); mProfileInfoCallback = callback; } @Override protected Person doInBackground(GoogleSignInAccount... params) { if (mActivityRef.get() == null){ SGLog.d(TAG+' GetProfileAsyncTask doInBackground activity is null.'); return null; } GoogleSignInAccount signInAccount = params[0]; Context context = mActivityRef.get().getApplicationContext(); GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( context, Collections.singleton(GENDER_SCOPE)); credential.setSelectedAccount(signInAccount.getAccount()); SGLog.d(TAG+' get profile info start.'); PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(ApkUtils.getAppName(context)) // your app name .build(); SGLog.d(TAG+' get profile info start.'); // Get info. on user Person person =null; try { person = service .people() .get('people/me') .setPersonFields('genders') .execute(); SGLog.d(TAG+' getPerson end.'); // return the result if (mProfileInfoCallback!=null){ mProfileInfoCallback.onGetProfileInfo(person); } } catch (Exception e) { SGLog.e(TAG+e.getMessage()); if (mProfileInfoCallback!=null){ mProfileInfoCallback.onGetProfileInfo(null); } e.printStackTrace(); } return person; } @Override protected void onPostExecute(Person aVoid) { super.onPostExecute(aVoid); } }

獲取性別信息

private void getProfileInfo(Person person){ SGLog.d(TAG+' executeProfileInfo...'); if (person == null){ notifyResult(mLastUser,Utils.SUCCESS); }else { try { List<Gender> genders = person.getGenders(); Gender gender = genders.get(0); String value = gender.getValue(); SGLog.d(TAG+' genders:'+genders.size()+ ' gender:'+value); mLastUser.setGender(value); notifyResult(mLastUser,Utils.SUCCESS); }catch (Exception e){ SGLog.e(TAG+' getProfileInfo error.'); notifyResult(null,SGErrorCode.LOGIN_FAILED); e.printStackTrace(); } } }

參考文獻(xiàn):

https://developers.google.com/identity/sign-in/android/sign-inhttps://developers.google.cn/android/guides/http-authhttps://developers.google.com/people/api/rest/?apix=truehttps://github.com/googlesamples/google-services/tree/master/android/signinhttps://developers.google.com/people/api/rest/v1/people/get

總結(jié)

到此這篇關(guān)于Android 集成 google 登錄并獲取 性別等隱私信息的文章就介紹到這了,更多相關(guān)Android 集成 google 登錄并獲取 性別等隱私信息內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 三级特黄视频 | 国产精品日韩欧美一区二区 | 国产美女三级做爰 | 手机看片久久高清国产日韩 | 久久一本色道综合 | 毛片免费看网站 | 色青五月天 | 国产只有精品 | 欧美性色黄大片在线观看 | 国产三级a三级三级三级 | 在线播放 亚洲 | 国产一区二区三区四区五区tv | 成人免费在线 | 久久中文字幕日韩精品 | 国产日韩精品视频一区二区三区 | 一级做a爰片久久毛片美女 一级做a爰片久久毛片免费看 | 精品久久国产老人久久综合 | 久久机热综合久久国产 | 久久爽久久爽久久免费观看 | 亚洲成a人在线观看 | 免费乱淫视频 | 欧美黑人巨大最猛性xxxxx | 国产dvd毛片在线视频 | 嫩草影院在线观看网站成人 | 免费看一级毛片欧美 | 中文字幕 亚洲精品 | a级日韩乱理伦片在线观看 a级特黄毛片免费观看 | 天天看片日本 | 国产最新精品 | 在线高清国产 | www成人免费视频 | 日韩一区二区三区在线观看 | 欧美成人亚洲高清在线观看 | 久久精品中文字幕不卡一二区 | 日本视频在线免费观看 | 天天看有黄有色大片 | 国产成人综合网亚洲欧美在线 | 99久久国产综合精品1尤物 | 深夜福利视频在线观看 | 久久一级黄色片 | 日韩精品在线一区 |