我正在尝试使用 OAuth 从 python 应用程序中获取Gmail atom 提要。我有一个可以下载 Google 阅读器提要的工作应用程序,我认为这应该只是更改范围和提要 URL 的问题。替换 URL 后,我仍然可以成功获取请求和访问令牌,但是当我尝试使用访问令牌获取提要时,我收到“401 Unauthorized”错误。这是我的简单测试程序:
import urlparse
import oauth2 as oauth
scope = "https://mail.google.com/mail/feed/atom/"
sub_url = scope + "unread"
request_token_url = "https://www.google.com/accounts/OAuthGetRequestToken?scope=%s&xoauth_displayname=%s" % (scope, "Test Application")
authorize_url = 'https://www.google.com/accounts/OAuthAuthorizeToken'
access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken'
oauth_key = "anonymous"
oauth_secret = "anonymous"
consumer = oauth.Consumer(oauth_key, oauth_secret)
client = oauth.Client(consumer)
# Get a request token.
resp, content = client.request(request_token_url, "GET")
request_token = dict(urlparse.parse_qsl(content))
print "Request Token:"
print " - oauth_token = %s" % request_token['oauth_token']
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
print
# Step 2: Link to web page where the user can approve the request token.
print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
print
raw_input('Press enter after authorizing.')
# Step 3: Get access token using approved request token
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, "POST")
access_token = dict(urlparse.parse_qsl(content))
print "Access Token:"
print " - oauth_token = %s" % access_token['oauth_token']
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
print
# Access content using access token
token = oauth.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
client = oauth.Client(consumer, token)
resp, content = client.request(sub_url, 'GET')
print content
您会注意到我使用“匿名/匿名”作为我的 OAuth 密钥/秘密,如未注册应用程序的 Google 文档中所述。这适用于谷歌阅读器,所以我看不出它不适用于 Gmail 的任何理由。有谁知道为什么这可能不起作用,或者我该如何解决它?谢谢。