【发布时间】:2012-04-22 19:37:30
【问题描述】:
我需要获取 OAuth2 身份验证令牌以将其传递给服务器,以便它可以获取用户的 Google 阅读器提要列表。服务器是 .NET - 我无法访问它或它的代码,但很可能它正在使用 unofficial Reader API
我可以使用 Android 帐户管理器通过以下代码为此目的获取有效令牌(注意 authTokenType="reader")
Account account = accounts[0];
manager.getAuthToken(account, "reader", null, this, new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
try {
// If the user has authorized your application to use the tasks API
// a token is available.
String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
// Now you can send the token to API...
cacheManager.putString(GOOGLE_AUTH, token);
GoogleReaderManager.startAddFeedActivity(AddGoogleReaderSourcesActivity.this);
finish();
} catch (OperationCanceledException e) {
Log.e(TAG, "User cancelled", e);
finish();
} catch (Exception e) {
Log.e(TAG, "Failed to obtain Google reader API_KEY", e);
}
}
}, null);
当我将令牌发送到服务器端 .Net 应用程序时,上面的代码工作正常:该应用程序能够检索阅读器提要列表。
问题在于这仅适用于“Google inside”设备。在 Nook 我没有这样的运气,因为我无法找到将 Google 帐户添加到客户经理的方法。所以我尝试使用 OAuth 2 协议作为described here
就获取令牌而言,它工作正常:用户从返回代码令牌的移动页面批准应用程序,然后移动应用程序交换身份验证令牌。但是,此令牌不适用于服务器进程。我有一种感觉,也许我在这个 URL 中使用了错误的范围:
我在各种组合中尝试过的范围:
- https://www.google.com/reader/api
- https://www.google.com/reader/api/0
- https://www.google.com/reader/api/0/subscription/list
- https://www.google.com/reader/api+https://www.google.com/reader/atom
这是从获取令牌 POST 返回的 JSON 示例
{"expires_in":3600,
"token_type":"Bearer",
"access_token":"ya29.AHES6ZSEvuUb6Bvd2DNoMnnN_UnfxirZmf_RQjn7LptFLfI",
"refresh_token":"1\/bUwa5MyOtP6VyWqaIEKgfPh08LNdawJ5Qxz6-qZrHg0"}
我是否弄乱了范围或令牌类型?不确定如何更改令牌类型。还有其他想法吗?
附:谷歌账号登录页面询问:Manage your data in Google Reader,所以我怀疑范围有误
【问题讨论】:
-
范围应为“google.com/reader/api”,如此处所述。消除中间人:尝试直接访问 API,当你让它工作时,然后使用你的 .NET 服务器。读者认证:code.google.com/p/google-reader-api/wiki/Authentication
-
这是我尝试的第一个示波器,唉,不行。
标签: android oauth-2.0 google-reader