Leo*_*onH 3 oauth-2.0 oauth2client google-oauth google-api-python-client gmail-api
我正在尝试验证我的凭据以访问GMail API.以前我使用run()
OAuth2中的方法和代码执行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 Code Online (Sandbox Code Playgroud)
该评论线是使用代码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)
你需要做的就是像这样将一个空的args列表传递给argparser
flags = tools.argparser.parse_args(args=[])
credentials = tools.run_flow(flow, storage, flags)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2263 次 |
最近记录: |