获取OAuth身份验证网址时出错

时间:2012-06-27 13:44:09

标签: python oauth-2.0 appharbor httplib2

我在使用AppHarbor API之前授权用户的Python桌面应用程序中有以下代码。我按照knowledge base中提到的步骤操作,并具有以下身份验证代码:

 def OnAuthenticate(self, event):
    client_id = "" # My App's client id
    client_secret_key = "" # My App's secret key
    consumer = oauth2.Consumer(key=client_id, secret=client_secret_key)

    request_token_url = "https://appharbor.com/user/authorizations/new?client_id="+client_id+"&redirect_uri=http://localhost:8095"

    client = oauth2.Client( consumer )
    resp, content = client.request(request_token_url, "GET")
    ...

但是,在发送请求时,响应不正确,这是错误:

client.request(request_token_url, "GET")
TypeError: must be string or buffer, not None

我在这里缺少什么东西吗?


编辑:以下是抛出的堆栈跟踪:

    resp, content = client.request(request_token_url, "GET")
    File "C:\Python27\Lib\site-packages\oauth2-1.5.211-py2.7.egg\oauth2\__init__.py", line 682, in request
    connection_type=connection_type)
    File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1544, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
    File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1342, in _request
   (response, content) = self.request(location, redirect_method, body=body, headers = headers, redirections = redirections - 1)
    File "C:\Python27\Lib\site-packages\oauth2-1.5.211-py2.7.egg\oauth2\__init__.py", line 662, in request
    req.sign_request(self.method, self.consumer, self.token)
    File "C:\Python27\Lib\site-packages\oauth2-1.5.211-py2.7.egg\oauth2\__init__.py", line 493, in sign_request
    self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())
    TypeError: must be string or buffer, not None

调试到调用后,我到达了发出请求的httplib2._request函数

(response, content) = self._conn_request(conn, request_uri, method, body, headers)

这导致以下页面的错误响应= 302(在内容对象中显示)

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://appharbor.com/session/new?returnUrl=%2Fuser%
 2Fauthorizations%2Fnew%3Foauth_body_hash%3D2jmj7l5rSw0yVb%252FvlWAYkK%252FYBwk%
 253D%26oauth_nonce%3D85804131%26oauth_timestamp%3D1340873274%
 26oauth_consumer_key%3D26bacb38-ce5a-4699-9342-8e496c16dc49%26oauth_signature_method%
 3DHMAC-SHA1%26oauth_version%3D1.0%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%
 253A8095%26client_id%3D26bacb38-ce5a-4699-9342-8e496c16dc49%26oauth_signature%
 3DXQtYvWIsvML9ZM6Wfs1Wp%252Fy3No8%253D">here</a>.</h2>
 </body></html>

该函数接下来从内容中删除了主体,以调用body设置为None的另一个请求,从而导致抛出错误。

(response, content) = self.request(location, redirect_method, body=body, headers = headers, redirections = redirections - 1)

1 个答案:

答案 0 :(得分:0)

我不熟悉Python lib,但您是否考虑过这是因为您需要让用户完成three-legged flow Twitter flow并使用您在问题中提到的url作为authorize_url?获得代码后,通过POST到此网址https://appharbor.com/tokens来检索令牌。

您可能还想仔细查看desktop OAuth .NET sample,以便更好地了解其工作原理。