【发布时间】:2014-10-14 20:43:07
【问题描述】:
我正在尝试验证我的凭据以访问 GMail API。以前我使用 OAuth2 中的 run() 方法和代码 credentials = tools.run(flow, STORAGE, http=http) 执行此操作,但现在这是一个已弃用的方法。我现在使用run_flow() 方法来验证我的凭据。
import httplib2
import argparse
from apiclient import errors
from apiclient.discovery import build
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
CLIENT_SECRET_FILE = 'your_client_secret.json'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.modify'
STORAGE = Storage('gmail.storage')
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()
credentials = STORAGE.get()there are credentials, no reauth is needed
#parser = argparse.ArgumentParser(parents=[tools.argparser])
#flags = parser.parse_args() #Put your arguments in the parenthesis
if credentials is None or credentials.access_token_expired:
credentials = run(flow, STORAGE, http=http)
#credentials = tools.run_flow(flow, STORAGE, flags, http=http)
http = credentials.authorize(http)
gmail_service = build('gmail', 'v1', http=http)
注释行是使用run_flow()而不是run()的代码。
注释掉的代码给了我错误:run.py: error: unrecognized arguments: AdminTests,AdminTests 不是我给 Python 的参数。
当我将解析的参数更改为 flags = parser.parse_args(['--noauth_local_webserver']) 时,我没有收到任何错误,但没有任何反应。
我应该使用哪个flag 尽可能接近地模拟run(),我应该如何解析它?
编辑:当使用 run() 方法验证我的凭据时,访问的 URL 是:
http://localhost:8080/?code=4/myuniqueID(示例中缺少我的唯一 ID)
【问题讨论】:
-
更多细节可以在这里找到...stackoverflow.com/questions/24890146/…
标签: oauth-2.0 google-oauth google-api-python-client gmail-api oauth2client