2012-07-14 69 views
2

我在一個情況下,我需要請求訪問令牌兩個範圍(從我的Android應用程序),要求多於一個的oauth2範圍https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/userinfo.userinfo通過的AccountManager在Android的

我想獲得這兩種權限在getAuthToken的單個調用中,但無法找出在authTokenType參數中傳遞的字符串。我試了合理的組合,沒有陽性結果:(

有沒有人解決這個問題呢?這可能嗎?

回答

6

如果需要多個的OAuth 2.0範圍,use a space-separated list

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

你問樣品代碼,所以看看Google Docs Upload Sample應用程序,並在此應用程序中查看在this sample Android screen中完成的身份驗證流程(忽略它是關於Google文檔,它仍然首先授權)。您可以獲取整個應用程序並在仿真器中運行它Google API可以在您的手機上展示或運行。授權流程與buttonAuthorize點擊,授權(開始),你在這個方法特別感興趣:

private void gotAccount(Account account) 
{ 

    Bundle options = new Bundle(); 

    accountManager.getAuthToken(
      account,      // Account retrieved using getAccountsByType() 
      "oauth2:https://www.googleapis.com/auth/userinfo.email oauth2:https://www.googleapis.com/auth/userinfo.userinfo",   // Auth scope 
      //"writely",   // Auth scope, doesn't work :(
      options,      // Authenticator-specific options 
      this,       // Your activity 
      new OnTokenAcquired(),   // Callback called when a token is successfully acquired 
      null); // Callback called if an error occurs 
} 

用戶得到該訪問請求畫面:

enter image description here

注意,這是使用'本地'OAuth2機制,不會打開網絡瀏覽器,而是使用首次激活Android手機時提供的身份驗證。

另外請注意,用戶看到的範圍,而不是一個友好的名稱的完整的URL,我haven't找到了解決這個問題的方法,如果你確實發現這將是偉大的,如果你能分享答案。

+0

我已經嘗試過,沒有成功:(,你有沒有使用此之前?如果是這樣,你能提供一個小的代碼片段展示瞭如何?謝謝! – tnacho 2012-07-14 17:44:44

+0

我已經更新了我的答案,包括對樣本的引用,試用了它。 – Mendhak 2012-07-14 18:19:17

+0

我再次嘗試,但沒有成功...幾乎複製了整個源代碼,但必須有其他東西...雖然這感覺像是正確的方向,謝謝! – tnacho 2012-07-14 20:48:50

10

我遇到了同樣的問題。沙阿幾乎是正確的,但他的範圍字符串是錯誤的。它應該是

"oauth2:<scope_url> <scope_url>" 

"oauth2:<scope_url> oauth2:<scope_url>"