通过的AccountManager在Android的
问题描述:
我在一个情况下,我需要请求访问令牌两个范围(从我的Android应用程序),要求多于一个的oauth2范围https://www.googleapis.com/auth/userinfo.email
和https://www.googleapis.com/auth/userinfo.userinfo
通过的AccountManager在Android的
我想获得这两种权限在getAuthToken
的单个调用中,但无法找出在authTokenType
参数中传递的字符串。我试了合理的组合,没有阳性结果:(
有没有人解决这个问题呢?这可能吗?
答
如果需要多个的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
}
用户得到该访问请求画面:
注意,这是使用'本地'OAuth2机制,不会打开网络浏览器,而是使用首次激活Android手机时提供的身份验证。
另外请注意,用户看到的范围,而不是一个友好的名称的完整的URL,我haven't找到了解决这个问题的方法,如果你确实发现这将是伟大的,如果你能分享答案。
答
我遇到了同样的问题。沙阿几乎是正确的,但他的范围字符串是错误的。它应该是
"oauth2:<scope_url> <scope_url>"
不
"oauth2:<scope_url> oauth2:<scope_url>"
我已经尝试过,没有成功:(,你有没有使用此之前?如果是这样,你能提供一个小的代码片段展示了如何?谢谢! – tnacho 2012-07-14 17:44:44
我已经更新了我的答案,包括对样本的引用,试用了它。 – Mendhak 2012-07-14 18:19:17
我再次尝试,但没有成功...几乎复制了整个源代码,但必须有其他东西...虽然这感觉像是正确的方向,谢谢! – tnacho 2012-07-14 20:48:50