2016-05-23 65 views

回答

1

按名稱是指電子郵件地址/用戶名或用戶的名稱。如果你正在尋找電子郵件,那麼你可以使用Users:getProfileClass

GET https://www.googleapis.com/gmail/v1/users/userId/profile 

樣品響應:

{ 
"emailAddress": string, 
"messagesTotal": integer, 
"threadsTotal": integer, 
"historyId": unsigned long 
} 

,但如果你想獲得用戶的名稱,你可以嘗試​​。

// Configure sign-in to request the user's ID, email address, and basic profile. ID and 
// basic profile are included in DEFAULT_SIGN_IN. 
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
.requestEmail() 
.build(); 

// Build a GoogleApiClient with access to GoogleSignIn.API and the options above. 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
.enableAutoManage(this, this) 
.addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
.build(); 

然後,點擊登錄按鈕時,啓動登錄意圖:

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent, RC_SIGN_IN); 

提示用戶選擇谷歌帳戶登錄。如果您請求概要文件,電子郵件和openid以外的範圍,則還會提示用戶授予對請求的資源的訪問權限。

最後,處理活動的結果:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 

// Result returned from launching the Intent from 
// GoogleSignInApi.getSignInIntent(...); 
if (requestCode == RC_SIGN_IN) { 
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
if (result.isSuccess()) { 
GoogleSignInAccount acct = result.getSignInAccount(); 
// Get account information 
mFullName = acct.getDisplayName(); 
mEmail = acct.getEmail(); 
} 
} 
} 

HTH

相關問題