Hey everyone,
I am developing a desktop twitter client in Python and Qt4 using
python-oauth2 for authentication. I am having issues getting an access
token using the verifier that the user enters via a dialog. I have
checked to make sure everything entered was correctly passed and
correctly type casted. The twitter server returns "invalid
oauth_verifier parameter" and the code for this function is as
follows:
@staticmethod
def sendRequest(method, httpmethod, args, auth=False):
settings = QSettings(Constants.COMPANY, Constants.PRODUCT)
params = urllib.urlencode(args)
if auth:
if not settings.value("oauth_token").toPyObject():
# Get request token from server
consumer =
oauth2.Consumer(Constants.OAUTH_CONSUMER_KEY, \
Constants.OAUTH_CONSUMER_SECRET)
client = oauth2.Client(consumer)
response, content =
client.request("http://api.twitter.com/oauth/
request_token", "GET")
request_token =
dict(urlparse.parse_qsl(content))
if response['status'] != "200":
print request_token
raise Exception("Failed to get request
token, server returned: %s
%s" % (response['status'], content))
# Open twitter authorization dialog and get PIN
if needed
if not
settings.value("oauth_verifier").toPyObject():
dialog =
TwitterAuthorizeDialog(request_token)
dialog.exec_()
settings.setValue("oauth_verifier",
dialog.getVerifier())
# Create new token with oauth verifier and
initialize the client
token =
oauth2.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(settings.value("oauth_verifier").toPyObject())
client = oauth2.Client(consumer, token)
# Lastly, get the access token from the server
response, content =
client.request("http://api.twitter.com/oauth/
access_token", "POST")
access_token = dict(urlparse.parse_qsl(content))
if response['status'] != "200":
print access_token
raise Exception("Failed to get access
token, server returned: %s
%s" % (response['status'], content))
# Reinitialize client using access token and
store the token
token =
oauth2.Token(access_token['oauth_token'],
access_token['oauth_token_secret'])
settings.setValue("oauth_token", token)
client = oauth2.Client(consumer,
settings.value("oauth_token").toPyObject())
response, content =
client.request("http://api.twitter.com%s.json"
% method, httpmethod, params)
return json.loads(response.read(), encoding="utf-8")
else:
try:
http = httplib.HTTPConnection("api.twitter.com")
http.request("GET", method + ".json", params)
response = http.getresponse()
if response.status != 200:
raise Exception("Failed to download
data, server returned: %d %s"
% (response.status, response.reason))
return None
return json.loads(response.read(),
encoding="utf-8")
except:
raise Exception("Failed to connect to the
twitter service.")
return None
Any help or suggestions would be greatly appreciated.
--
Alec Hussey
--
Subscription settings:
http://groups.google.com/group/twitter-development-talk/subscribe?hl=en