2014-04-14 93 views
2

我需要獲取訪問令牌並將其發送到服務器。使用該訪問令牌服務器應獲取所有用戶詳細信息,如姓名,個人資料圖片和電子郵件。Google plus登錄訪問令牌retriew

我可以使用Scopes.PLUS_LOGINScopes.PLUS_ME訪問令牌,但與訪問令牌服務器無法獲得用戶的電子郵件。

這裏是我的代碼:

@Override 
public void onConnected(Bundle arg0) { 
    mSignInClicked = false; 


    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 
      String token = null; 
      String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME; 
      try { 
       token = GoogleAuthUtil.getToken(
         getApplicationContext(), 
         Plus.AccountApi.getAccountName(mGoogleApiClient), 
         scope); 
       appUser.setToken(token); 
      } catch (IOException transientEx) { 
       // Network or server error, try later 
       Log.e(TAG, transientEx.toString()); 
      } catch (UserRecoverableAuthException e) { 
       // Recover (with e.getIntent())     
      } catch (GoogleAuthException authEx) { 
       // The call is not ever expected to succeed 
       // assuming you have already verified that 
       // Google Play services is installed. 
       Log.e(TAG, authEx.toString()); 
      } 

      return token; 
     } 

     @Override 
     protected void onPostExecute(String token) { 
      Log.i(TAG, "Access token retrieved:" + appUser.getToken()); 
      // Get user's information 
     } 

    }; 


} 

是否有人知道如何解決這個問題?

+0

我想使用您的代碼。你能解釋我什麼是appUser和setToken()嗎? –

+0

它是POJO類和二傳手和吸氣劑。你可以保存沒有的結果。 – Zookey

回答

2

你缺少範圍

https://www.googleapis.com/auth/userinfo.email 

我測試的其他範圍內,只有一個看似回報用戶的電子郵件。您可以測試不同的範圍和他們返回的內容:People: get

注意:我不是一個android程序員,你可能會有更好的運氣找出如何用android請求該範圍。我正在尋找,但一直未能找到它。

看起來像範圍可能只是電子郵件https://developers.google.com/+/api/oauth#email

+0

我的最終範圍應該怎樣看? String scope =「oauth2:」+ Scopes.PLUS_LOGIN +「」+ Scopes.PLUS_ME +「https://www.googleapis.com/auth/userinfo.email」; – Zookey

+0

這可能不會起作用。你需要找到適用於android的正確範圍。這就是api使用的實際範圍。 – DaImTo

+0

看看是否有一個名爲Scopes.email或類似 – DaImTo