2012-04-22 83 views
1

我需要获取OAuth2身份验证令牌才能将其传递到服务器,以便它可以为用户提取Google阅读器订阅源列表。服务器是.NET - 我无法访问它或它的代码,但最有可能的是它使用unofficial Reader APIAndroid - 无法使用OAuth访问令牌检索Google阅读器订阅源

我能够使用Android帐户管理器获取有效的令牌用于此目的使用以下代码(请注意,authTokenType = “读者”)

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内部”设备。在努克,我没有这样的运气,因为我无法找到将Google帐户添加到客户经理的方式。所以我试图使用OAuth 2协议作为described here

只要获得令牌,它就可以正常工作:用户从移动页面中批准应用,该页面返回代码令牌,然后移动应用交换Auth令牌。但是,此令牌不适用于服务器进程。我有,也许我使用了错误的范围在此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

这里是JSON的例子是从获得令牌POST

{"expires_in":3600, 
    "token_type":"Bearer", 
    "access_token":"ya29.AHES6ZSEvuUb6Bvd2DNoMnnN_UnfxirZmf_RQjn7LptFLfI", 
    "refresh_token":"1\/bUwa5MyOtP6VyWqaIEKgfPh08LNdawJ5Qxz6-qZrHg0"} 

上午我搞乱范围或令牌类型退换吗?不知道如何更改令牌类型。任何其他想法?

P.S.谷歌帐户登录页面要求:Manage your data in Google Reader,这就是为什么我怀疑范围是错误的

+0

如此处所述,范围应为'https://www.google.com/reader/api/'。消除中间人:尝试直接访问API,并且当它启动时,然后使用.NET服务器。读者身份验证:http://code.google.com/p/google-reader-api/wiki/Authentication – 2012-04-23 01:57:48

+0

这是我尝试的第一个范围,唉,不行。 – Bostone 2012-04-23 04:47:50

回答

0

我得到它的工作https://www.google.com/reader/api/0/subscription/list。所以想到与你分享。

本人有效access_token

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

  • 谷歌提供的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; 
    } 
    

所以此工程为获得订阅列表中正确的罚款;但一直未能使它适用于您在问题中提到的reader api。

让我知道,如果你有解决方案谷歌阅读器API。

相关问题