1

我需要获取 OAuth2 身份验证令牌以将其传递给服务器,以便它可以为用户获取 Google 阅读器提要列表。服务器是 .NET - 我无法访问它或它的代码,但很可能它使用的是非官方的 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 协议,如此处所述

就获取令牌而言,它工作正常:用户从返回代码令牌的移动页面批准应用程序,然后移动应用程序交换身份验证令牌。但是,此令牌不适用于服务器进程。我有一种感觉,也许我在这个 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"}

我弄乱了范围或令牌类型吗?不确定如何更改令牌类型。还有其他想法吗?

PS谷歌账号登录页面问:Manage your data in Google Reader,这就是我怀疑范围错误的原因

4

1 回答 1

0

我让它为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,请告诉我。

于 2013-02-19T06:41:21.983 回答