0

I have the following code in a Python desktop application that authorizes users before using the AppHarbor API. I am following the steps mentioned in the knowledge base and have the following authentication code:

 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")
    ...

However, on sending the request, the response is incorrect, this is the error:

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

Is there something that I am missing here?


Edit: Following is the stack trace that is thrown up:

    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

Upon debugging into the call, I reached httplib2._request function that issued a request

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

This resulted in the following page with error response = 302 (presented in content object)

<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>

The function next removed the body from the content to call another request with body set to None, resulting in the error that was thrown.

(response, content) = self.request(location, redirect_method, body=body, headers = headers, redirections = redirections - 1)
j0k
  • 22,600
  • 28
  • 79
  • 90
Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31
  • You should have a traceback with the exact file and line where the exception is raised, as well as the full call stack. Maybe you could post it ? (or even read it, chances here it will answer your question). – bruno desthuilliers Jun 27 '12 at 14:21

1 Answers1

0

I'm not familiar with the Python lib, but have you considered whether this is because you need to take the user through the three-legged flow Twitter flow and use the url you mention in your question as the authorize_url? Once you have the code, you retrieve the token by POST'ing to this url: https://appharbor.com/tokens.

You might also want to take a closer look at the desktop OAuth .NET sample to get a better understanding of how this works.

friism
  • 19,068
  • 5
  • 80
  • 116