2012-04-22 44 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。

相關問題