Android 整合 google 登入並獲取性別等隱私資訊的實現程式碼
前言
公司做海外產品的,整合的是 google 賬號登入,賬號資訊、郵箱等這些不涉及隱私的按 google 的正常登入流程可以輕鬆實現 。但是一旦需要獲取涉及隱私的資訊就比較麻煩,文件也不是十分清晰,非常難找,很多坑。
google 賬號登入
官方連結:https://developers.google.com/identity/sign-in/android/start
https://developers.google.com/identity/sign-in/android/sign-in
google 賬號登入接入的坑:
- 申請的
client_id
必須是 api console 後臺 :https://console.cloud.google.com/apis 與 google play 後臺對應的應用關聯起來。 client_id
下的簽名信息和報名資訊必須和測試時的 apk 的簽名信息和報名資訊一致。- 在 google play 下啟動 google 的二次簽名,則 api console 後臺的簽名信息是二次簽名後的資訊。打包測試時使用上傳 到 Google play 後臺的 apk 的簽名證書即可。
google 登入的流程在這個文件寫的比較清楚了:https://developers.google.com/identity/sign-in/android/sign-in,這裡大致說一下,不貼程式碼了
構建需求請求的內容:
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.發起登入請求,跳轉 google 登入頁面。
Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent,RC_SIGN_IN);
獲取 Google 登入返回
@Override public 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,傳到你自己的 服務端 做驗證
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()); } }
切換賬號
/** * 重新獲取賬號列表 */ 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(); } }
獲取公開資料和需要特別授權的資訊(性別、生日等)
1、在構建請求是新增獲取 的公共資料資訊 及 需要獲取的特殊資訊
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);
需要請求的資訊可在如下連結查詢:https://developers.google.com/people/api/rest/v1/people/get
2、檢測是否有許可權
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); }
注意:這一步不需要也可以,有這一步會出現一個 “再確認” 的授權頁面,沒有也不影響獲取的資訊。
3、跳轉登入頁面 (同以上 google 賬號登入)
4、獲取登入資訊 (同以上 Google賬號登入)
5、開啟執行緒獲取 特殊資訊
getProfileAsyncTask = new GetProfileAsyncTask(mActivity,new GpProfileInfoCallback() { @Override public void onGetProfileInfo(Person person) { SGLog.d(TAG+" onGetProfileInfo... "); getProfileInfo(person); } }); getProfileAsyncTask.execute(signInAccount);
非同步任務
// 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(); } } }
參考文獻:
https://developers.google.com/identity/sign-in/android/sign-in
https://developers.google.cn/android/guides/http-auth
https://developers.google.com/people/api/rest/?apix=true
https://github.com/googlesamples/google-services/tree/master/android/signin
https://developers.google.com/people/api/rest/v1/people/get
總結
到此這篇關於Android 整合 google 登入並獲取 性別等隱私資訊的文章就介紹到這了,更多相關Android 整合 google 登入並獲取 性別等隱私資訊內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!