Google OAuth问题:invalid_signature

时间:2011-07-27 21:25:53

标签: python oauth

我正在努力让OAuth在一个小的Python脚本中工作。我正在使用python-oauth2,我正在尝试连接到Google以列出我的Google文档。

获取请求令牌并授权令牌似乎工作正常,但每当我尝试获取访问令牌时,我都会收到HTTP 400响应,响应内容为“signature_invalid”。

这是我的代码:

import httplib2, os, sys, tempfile, urllib, urlparse
import oauth2 as oauth
#httplib2.debuglevel=1

# this php script writes oauth_verifier to /tmp/verifier.txt
oauth_callback = 'http://localhost/api.php'

scope = 'https://docs.google.com/feeds/'
xoauth_displayname = 'Adam\'s API Test'
url = 'https://www.google.com/accounts/OAuthGetRequestToken?scope=%s&oauth_callback=%s&xoauth_displayname=%s' % (scope, oauth_callback, xoauth_displayname)

######## OAUTH: GET REQUEST TOKEN #############
consumer = oauth.Consumer('anonymous','anonymous')
client = oauth.Client(consumer)
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'OAuthGetRequestToken OK'
else:
    print 'OAuthGetRequestToken status: %s' % resp['status']
    print content
    sys.exit(1)

######## OAUTH: AUTHORIZE TOKEN ###############
oauth_token = urlparse.parse_qs(content)['oauth_token'][0]
url = 'https://www.google.com/accounts/OAuthAuthorizeToken?hd=default&oauth_token=%s' % urllib.quote_plus(oauth_token)
print 'Visit this URL in your browser: %s' % url
raw_input('Press ENTER once you have granted access...')

######## OAUTH: GET ACCESS TOKEN ##############
verifier = file('/tmp/verifier.txt').read().rstrip()
url = 'https://www.google.com/accounts/OAuthGetAccessToken?oauth_token=%s&oauth_verifier=%s' % (oauth_token, verifier)
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'OAuthGetAccessToken OK'
    print content
else:
    print 'OAuthGetAccessToken status: %s' % resp['status']
    print content
    sys.exit(1)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在伊娃的帮助下,我修复了它! This post had the solution

我未能使用oauth_token_secret,从原始请求中返回未经授权的令牌。我错过了Consumer被重用,但是在Twitter Three-legged OAuth Example in the python-oauth2 README结尾处为访问令牌提取实例化了一个新的Client

以下是修复上述代码的补丁:

--- old.py  2011-07-28 10:38:06.904639958 -0500
+++ new.py  2011-07-28 10:38:44.192639954 -0500
@@ -22,6 +22,7 @@

 ######## OAUTH: AUTHORIZE TOKEN ###############
 oauth_token = urlparse.parse_qs(content)['oauth_token'][0]
+oauth_token_secret = urlparse.parse_qs(content)['oauth_token_secret'][0]
 url = 'https://www.google.com/accounts/OAuthAuthorizeToken?hd=default&oauth_token=%s' % urllib.quote_plus(oauth_token)
 print 'Visit this URL in your browser: %s' % url
 raw_input('Press ENTER once you have granted access...')
@@ -29,6 +30,7 @@
 ######## OAUTH: GET ACCESS TOKEN ##############
 verifier = file('/tmp/verifier.txt').read().rstrip()
 url = 'https://www.google.com/accounts/OAuthGetAccessToken?oauth_token=%s&oauth_verifier=%s' % (oauth_token, verifier)
+client.token = oauth.Token(oauth_token, oauth_token_secret)
 resp, content = client.request(url, 'GET')
 if resp['status'] == '200':
     print 'OAuthGetAccessToken OK'

这是原始代码,应用了该补丁:

import httplib2, os, sys, tempfile, urllib, urlparse
import oauth2 as oauth
#httplib2.debuglevel=1

# this php script writes oauth_verifier to /tmp/verifier.txt
oauth_callback = 'http://localhost/api.php'

scope = 'https://docs.google.com/feeds/'
xoauth_displayname = 'Adam\'s API Test'
url = 'https://www.google.com/accounts/OAuthGetRequestToken?scope=%s&oauth_callback=%s&xoauth_displayname=%s' % (scope, oauth_callback, xoauth_displayname)

######## OAUTH: GET REQUEST TOKEN #############
consumer = oauth.Consumer('anonymous','anonymous')
client = oauth.Client(consumer)
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'OAuthGetRequestToken OK'
else:
    print 'OAuthGetRequestToken status: %s' % resp['status']
    print content
    sys.exit(1)

######## OAUTH: AUTHORIZE TOKEN ###############
oauth_token = urlparse.parse_qs(content)['oauth_token'][0]
oauth_token_secret = urlparse.parse_qs(content)['oauth_token_secret'][0]
url = 'https://www.google.com/accounts/OAuthAuthorizeToken?hd=default&oauth_token=%s' % urllib.quote_plus(oauth_token)
print 'Visit this URL in your browser: %s' % url
raw_input('Press ENTER once you have granted access...')

######## OAUTH: GET ACCESS TOKEN ##############
verifier = file('/tmp/verifier.txt').read().rstrip()
url = 'https://www.google.com/accounts/OAuthGetAccessToken?oauth_token=%s&oauth_verifier=%s' % (oauth_token, verifier)
client.token = oauth.Token(oauth_token, oauth_token_secret)
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'OAuthGetAccessToken OK'
    print content
else:
    print 'OAuthGetAccessToken status: %s' % resp['status']
    print content
    sys.exit(1)

请注意,实际上,oauth_tokenoauth_verifier不需要手动添加到查询字符串中以获取访问令牌(python-oauth2为我们执行此操作)。我按原样保留了代码,因此我可以说明从原始帖子中的非工作代码到本答案中的工作代码的最简单的更改。最终的网址可以是https://www.google.com/accounts/OAuthGetAccessToken

最终打印内容吐出oauth_tokenoauth_token_secret。这些参数用于后续API调用,例如在Google Docs上获取文档列表。

以下是执行此操作的示例代码:

import httplib2, os, sys, tempfile, urllib, urlparse
import oauth2 as oauth

######## OAUTH: GET REQUEST TOKEN #############
consumer = oauth.Consumer('anonymous', 'anonymous')
creds = {'oauth_token_secret': 'INSERT_SECRET_FROM_ABOVE', 'oauth_token': 'INSERT_TOKEN_FROM_ABOVE'}
client = oauth.Client(consumer)
client.token = oauth.Token(creds['oauth_token'], creds['oauth_token_secret'])
url = 'https://docs.google.com/feeds/default/private/full?v=3'
resp, content = client.request(url, 'GET')
if resp['status'] == '200':
    print 'list status OK'
    fh = open('/tmp/list.xml', 'w')
    fh.write(content)
    fh.close()
else:
    print 'list status: %s' % resp['status']
    print content
    sys.exit(1)