6

OAuth を使用して Python アプリケーションからGmail アトム フィードを取得しようとしています。私は、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

未登録アプリケーションの Google ドキュメント に記載されているように、OAuth キー/シークレットとして「anonymous/anonymous」を使用していることに気付くでしょう。これは Google リーダーでは問題なく機能するため、Gmail では機能しない理由がわかりません。なぜこれが機能しないのか、またはどのようにトラブルシューティングを行うことができるのかについて、誰かが何か考えを持っていますか? ありがとう。

4

1 に答える 1

3

ATOM フィードを使用する代わりに、OAuth を使用して Google の IMAP サーバーにアクセスしてみてください。少しグーグルした後、私はこれを見つけまし

「Gmail は、XOAUTH と呼ばれる標準を介して IMAP および SMTP 上の OAuth をサポートしています。これにより、OAuth トークンとシークレットを使用して、Gmail の IMAP および SMTP サーバーに対して認証を行うことができます。また、通常の SMTP および IMAP ライブラリを使用できるという追加の利点もあります。 python-oauth2 パッケージは、XOAUTH を実装し、imaplib.IMAP4_SSL と smtplib.SMTP をラップする IMAP と SMTP ライブラリの両方を提供します。これにより、標準の Python ライブラリを使用して OAuth 資格情報で Gmail に接続できます。」

http://github.com/simplegeo/python-oauth2から

于 2010-07-03T19:19:58.783 に答える