2012-05-12 74 views
5

我想從AccountManager獲取一個Google Authtoken,我可以發送到我的Web服務(不是在App Engine上託管)來認證用戶(我只需要電子郵件地址,最終他名稱,如果沒有這個要求的權限)。從AccountManager獲取基本的谷歌認證令牌

我必須使用「getAuthToken」方法的「authTokenType」參數嗎?

而我必須使用哪個google Api來獲取用戶電子郵件?

+0

剛發現另一個在Stackoverflow上似乎適合的答案:http://stackoverflow.com/a/6680837 –

回答

4

這是可行的使用OpenID連接,但是這有點實驗性的,所以細節可能會在未來改變。如果您獲得了「https://www.googleapis.com/auth/userinfo.email」或「https://www.googleapis.com/auth/userinfo.profile」範圍的OAuth令牌,則可以使用它獲取來自https://www.googleapis.com/oauth2/v1/userinfo的用戶信息(包括電子郵件)。當然,用戶需要對此進行授權。

您理論上應該能夠使用從AcccountManager獲得令牌「的oauth2:HTTPS://www.googleapis.com/auth/userinfo.profile」作爲標記類型,但不會出現上下工夫我的設備(Galaxy Nexus with stock 4.0.4)。由於通過AccountManager獲取令牌不起作用(至少現在),唯一可靠的方法是使用WebView並通過瀏覽器獲取,如下所述:https://developers.google.com/accounts/docs/MobileApps

這裏有一個演示Web應用程序,是否這樣:https://oauthssodemo.appspot.com

(晚)更新:Google Play服務已經發布,它是獲取OAuth令牌的首選方式。它應該適用於Android 2.2及更高版本的所有設備。獲取個人資料令牌確實有效,實際上他們在演示應用程序中使用它

+0

是否有可用於https://www.googleapis.com/auth/userinfo.email或https://www.googleapis.com/auth/userinfo.profile的別名(否則此網址將顯示在權限中請求,我不認爲是非常用戶友好的)? –

+0

沒有我知道的。你設法得到一個令牌嗎?正如我所說,至少在我的設備上看起來似乎不起作用。 –

+0

好的謝謝你的信息,非常有趣。我需要一種可以跨越所有設備的解決方案,所以這種方法對於這種方法並不合適。我現在能想到的唯一可能的解決方案是獲取一個應用引擎令牌,並擁有一個額外的應用引擎後端,它將提供一個認證服務。我真的不喜歡這種方法。 –

0

您可以通過Google+人員API獲取用戶名。 (它不會提供用戶的電子郵件地址)。

如果這樣可以,您可以使用「知道您在Google上的人」作爲authTokenType。

Google提供了一個示例應用程序,演示如何將Android帳戶管理器與Google+ API結合使用。

鏈接:http://code.google.com/p/google-plus-java-starter/source/browse/#hg%2Fandroid

+0

謝謝,但我需要一個令牌,我可以用它來獲取用戶的電子郵件,因爲這是後端識別用戶的方式。 –

2

我也遇到過這個問題,因爲我無法找到任何參考資料。或許,這可以幫助你(代碼從Android例如複製使用該客戶經理):

  1. 某處在你的Android應用程序的事件處理程序,發出一個身份驗證令牌的請求,以獲取用戶的電子郵件地址安卓:

    _accountMgr = AccountManager.get(this); 
    Account [] accounts = _accountMgr.getAccounts();     
    Account account = accounts[0]; // For me this is Google, still need to figure out how to get it by name. 
    _accountMgr.getAuthToken(account, AUTH_TOKEN_TYPE, false, new GetAuthTokenCallback(), null); 
    
  2. 在回調,提取訪問令牌:

    private class GetAuthTokenCallback implements AccountManagerCallback<Bundle> { 
        public void run(AccountManagerFuture<Bundle> result) { 
         Bundle bundle; 
         try { 
          bundle = result.getResult(); 
          final String access_token = bundle.getString(AccountManager.KEY_AUTHTOKEN); 
          // store token somewhere you can supply it to your web server. 
         } catch (Exception e) { 
          // do something here. 
         } 
        } 
    } 
    
  3. 使一些請求到Web服務器,提供了ACC ess令牌。

  4. 在Web服務器,驗證訪問令牌和獲得的電子郵件地址:

    curl -d 'access_token=<this is the token the app sent you>' https://www.googleapis.com/oauth2/v1/tokeninfo 
    

    你應該得到這樣的事情:

    { 
        "issued_to": "<something>.apps.googleusercontent.com", 
        "audience": "<something>.apps.googleusercontent.com", 
        "scope": "https://www.googleapis.com/auth/userinfo.email", 
        "expires_in": 3562, 
        "email": "<users email address>", 
        "verified_email": true, 
        "access_type": "online" 
    } 
    

    ,或者如果出了問題:

    { 
        "error": "invalid_token", 
        "error_description": "Bad Request" 
    }