2017-09-25 274 views
0

我正在嘗試使用Google登錄用戶。Google signin Android中的問題

我得到了演示代碼登錄,它工作正常。

場景1:用戶使用已有帳戶登錄。 預期的解決方案:用戶選擇他/她的帳戶後,並在onActivityResult我應該得到選定的電子郵件帳戶的詳細信息,如名稱,圖片,電子郵件等等.....這是未來的正確。

情景2:用戶可以添加新帳戶並以音頻方式登錄。 預期解決方案:當用戶點擊登錄谷歌,並選擇創建新的帳戶,而不是選擇已有的帳戶。用戶將進入添加新賬戶頁面並插入他們的憑證,並且在成功添加新賬戶後,用戶應自動登錄。

錯誤:但是當用戶添加新帳戶並返回到登錄頁面。我能夠獲得電子郵件,身份證但givenName,並且familyName爲空。我需要用戶的名字。 現在帳戶被添加到設備中,接下來當用戶從登錄頁面中選擇新添加的帳戶時,此時在onActivityResult中,我正在獲取用戶的NAME。

什麼,我想,當用戶添加一個新的帳戶或選擇現有帳戶,它應該顯示我的用戶

這裏的名稱代碼handleSignIn

LoginActivity.java

public void onStart() { 
    super.onStart(); 

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); 
    if (opr.isDone()) { 
     // If the user's cached credentials are valid, the OptionalPendingResult will be "done" 
     // and the GoogleSignInResult will be available instantly. 
     Log.d(TAG, "Got cached sign-in"); 
     GoogleSignInResult result = opr.get(); 
     handleSignInResult(result); 
    } else { 
     // If the user has not previously signed in on this device or the sign-in has expired, 
     // this asynchronous branch will attempt to sign in the user silently. Cross-device 
     // single sign-on will occur in this branch. 
     opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { 
      @Override 
      public void onResult(GoogleSignInResult googleSignInResult) { 
       handleSignInResult(googleSignInResult); 
      } 
     }); 
    } 
} 


@Override 
protected void onCreate(Bundle savedInstanceState){ 
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .build(); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
} 


protected void onActivityResult(
     final int requestCode, 
     final int resultCode, 
     final Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleSignInResult(result); 
    } else 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
} 



private void handleSignInResult(GoogleSignInResult result) { 
    Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
    if (result.isSuccess()) { 
     // Signed in successfully, show authenticated UI. 
     GoogleSignInAccount acct = result.getSignInAccount(); 
     Log.e(TAG, "handleSignInResult: getPhotoUrl " + acct.getPhotoUrl()); 
     Log.e(TAG, "handleSignInResult: getServerAuthCode " + acct.getServerAuthCode()); 
     Log.e(TAG, "handleSignInResult: getIdToken " + acct.getIdToken()); 
     Log.e(TAG, "handleSignInResult: getId " + acct.getId()); 
     Log.e(TAG, "handleSignInResult: getGivenName " + acct.getGivenName()); //firstName and lastName="" 
     Log.e(TAG, "handleSignInResult: getDisplayName " + acct.getDisplayName()); 
     Log.e(TAG, "handleSignInResult: getEmail " + acct.getEmail()); 
     Log.e(TAG, "handleSignInResult: getFamilyName " + acct.getFamilyName()); 
     Log.e(TAG, "handleSignInResult: getAccount " + acct.getAccount()); 
     Set<Scope> scopeSet = acct.getGrantedScopes(); 
     for (Scope scope : scopeSet) { 
      Log.e(TAG, "handleSignInResult: " + scope.zzpp()); 
     } 
    } 
} 

回答

0

我加入這個代碼

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .requestIdToken("Web client (auto created by Google Service)") // added this line 
      .requestProfile() 
      .build(); 

Web客戶端可在https://console.developers.google.com找到並轉至您的應用程序。

在左邊選擇憑據,你會發現Web客戶端的OAuth 2.0客戶端ID部分。

相關問題