我正在尝试让OAuth在一个小的Python脚本中工作。我正在使用python-oauth2,并且我正在尝试连接到谷歌来列出我的谷歌文档。
获取请求令牌和授权令牌似乎工作得很好,但是每当我尝试获取访问令牌时,都会得到HTTP400响应,并且响应内容是"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)有什么想法吗?
发布于 2011-07-29 01:52:03
在伊娃的帮助下,我修好了!This post had the solution。
我无法利用从原始请求未授权令牌返回的oauth_token_secret。我没有注意到Consumer正在被重用,但是正在实例化一个新的Client,用于在Twitter Three-legged OAuth Example in the python-oauth2 README的末尾获取访问令牌。
下面是修复上述代码的补丁:
--- 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_token和oauth_verifier手动添加到查询字符串中来获取访问令牌(python-oauth2会为我们做这件事)。我让代码保持原样,这样我就可以说明从原始帖子中的非工作代码到这个答案中的工作代码的最简单的更改。最终的URL可以是简单的https://www.google.com/accounts/OAuthGetAccessToken。
最后打印出来的内容是oauth_token和oauth_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)https://stackoverflow.com/questions/6851428
复制相似问题