【问题标题】:Android - unable to use OAuth access token to retrieve Google Reader feedsAndroid - 无法使用 OAuth 访问令牌来检索 Google 阅读器提要
【发布时间】: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://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.google.com/reader/api/0/subscription/list&redirect_uri=http://localhost&approval_prompt=force&state=/ok&client_id={apps.client.id}

我在各种组合中尝试过的范围:

  1. https://www.google.com/reader/api
  2. https://www.google.com/reader/api/0
  3. https://www.google.com/reader/api/0/subscription/list
  4. 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,所以我怀疑范围有误

【问题讨论】:

标签: android oauth-2.0 google-reader


【解决方案1】:

我让它为https://www.google.com/reader/api/0/subscription/list 工作。所以想和大家分享一下。

我有有效的access_token

这是我试图解决的问题(部分):

  • Google 提供 OAuth 2.o playgound;他们实际上模拟了 OAuth 2.0 的所有方面以及最终的 API 调用以获取数据。 我发现这非常有帮助,因为它清楚地显示了发送请求的内容。 这是网址:https://developers.google.com/oauthplayground/
  • 使用它,我在下面调整了我的 api 调用,它可以工作:)

    public static  String getReaderContent(String accessToken){
       String url = "https://www.google.com/reader/api/0/subscription/list" ; 
    
       HttpClient client = new HttpClient();
    
       GetMethod method = new GetMethod(url);
       String response="";
       method.setRequestHeader("Authorization", "OAuth "+accessToken);
       try {
         int statusCode = client.executeMethod(method);
         String response= method.getResponseBodyAsString();
    
         System.out.println("response " + responseStr);
       } catch (HttpException e) {
         e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();
      }
    return response;
    }
    

所以这对于获取订阅列表很有效;但无法使其适用于您在问题中提到的阅读器 api。

如果你有办法绕过谷歌阅读器 API,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2012-09-25
    • 2013-05-26
    相关资源
    最近更新 更多