2

我正試圖通過應用程序訪問用戶列表。我無法獲得兩腳認證。我希望能夠在安裝後無需用戶許可的情況下訪問Google的API。 gdata lib在今年早些時候已棄用,但業務要求需要在域管理員安裝應用程序之後無需用戶許可即可訪問API。如何在python gdata庫中使用雙腿oauth?

這是我迄今所做的:

  • 我已經建立在Marketplace上的應用程序。該應用程序清單聲明此範圍:

    https://apps-apis.google.com/a/feeds/user/

  • 我已經安裝了應用程序的測試域和上述範圍授予的權限。

  • 我已經在這裏看到的樣品:我使用Python TwoLeggedOAuthExample.py

https://code.google.com/p/gdata-python-client/source/browse/samples/oauth/

  • 棄用Service方法:2_legged_oauth.py
  • 更新Client方法2.7.5和gdata 2.0.18。這是我的代碼,它被改編從上面提到的例子:

    import gdata.gauth 
    import gdata.apps.client 
    
    CONSUMER_KEY = "[snip].apps.googleusercontent.com" 
    CONSUMER_SECRET = "[snip]" 
    requestor_id = '[email protected]' 
    
    client = gdata.apps.client.AppsClient(domain='mydomain.com', source='myapp-v1') 
    client.auth_token = gdata.gauth.TwoLeggedOAuthHmacToken(
        CONSUMER_KEY, CONSUMER_SECRET, requestor_id) 
    client.ssl = True 
    
    # Failure occurs here. 
    feed = client.RetrieveAllUsers() 
    

    響應:

    <HTML> 
    <HEAD> 
    <TITLE>Unknown authorization header</TITLE> 
    </HEAD> 
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> 
    <H1>Unknown authorization header</H1> 
    <H2>Error 401</H2> 
    </BODY> 
    </HTML> 
    

    下面是被髮送的報頭中的細節:

    GET /a/feeds/mydomain.com/user/2.0?xoauth_requestor_id=myuser%40mydomain.com HTTP/1.1 
    Host: apps-apis.google.com 
    Accept-Encoding: identity 
    GData-Version: 2.0 
    Authorization: OAuth oauth_nonce="035378574830673", oauth_timestamp="1383593346", oauth_consumer_key="[snip].apps.googleusercontent.com", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="[snip]" 
    User-Agent: test-test-v1 gdata-py/2.0.18 
    

    這裏是調試有關響應的信息(如將atom.http_core.ProxiedHttpClient放入調試模式):

    reply: 'HTTP/1.1 401 Unknown authorization header\r\n' 
    header: WWW-Authenticate: GoogleLogin realm="http://www.google.com/accounts/ClientLogin", service="apps" 
    header: Content-Type: text/html; charset=UTF-8 
    header: Date: Mon, 04 Nov 2013 19:33:35 GMT 
    header: Expires: Mon, 04 Nov 2013 19:33:35 GMT 
    header: Cache-Control: private, max-age=0 
    header: X-Content-Type-Options: nosniff 
    header: X-Frame-Options: SAMEORIGIN 
    header: X-XSS-Protection: 1; mode=block 
    header: Server: GSE 
    header: Alternate-Protocol: 443:quic 
    header: Transfer-Encoding: chunked 
    

    如何使用雙腿oauth訪問域上的用戶?我願意使用較新的Google API客戶端(oauth2clientapiclient),但前提是我可以使用雙重身份驗證,因爲我的業務要求沒有第三方。

回答

1

我建議您對readthedocs瞭解有關OAuth2服務帳戶的信息。

2-legged-OAuth不推薦使用,gdata APIs在很大程度上被棄用,並且便利的服務更清潔和更易於使用。

+0

我的印象是由於以下原因我無法使用服務帳戶: 1. http://goo.gl/NDZLj2 - 僅支持某些API(> 1歲) 2.您的鏈接http://goo.gl/fj9KdX表示「在不訪問用戶信息的情況下代表應用程序行事」。我相信我需要訪問用戶信息。 3. This SO q/a also said that I can access access user info with a service account:http://goo.gl/utY5us – jon

+0

如果我可以使用服務帳戶訪問我想要的(Admin SDK,Directory API),那麼我寧願使用它,我只是覺得它不被支持,因爲我將訪問用戶數據。這不準確嗎? – jon

+0

我唯一厭惡使用新的庫和API的是我有一個業務需求,防止OAuth2流的第三條腿(額外的用戶同意)。 – jon