如何从备用源创建Google API OAuth凭据对象

oli*_*oil 7 python google-api oauth-2.0

我正在使用这个简单的Google API示例:

import httplib2

from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run

# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'

# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'

# Location of the credentials storage file
STORAGE = Storage('gmail.storage')

# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, STORAGE, http=http)

# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)

# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
Run Code Online (Sandbox Code Playgroud)

并且看到我之前已经完成了OAuth流程(在不同的非Python应用程序中)并拥有我的刷新令牌等.我想跳过此示例的第一部分并手动创建预期的存储文件gmail.storage或创建凭证对象以其他方式.

问题是我找不到任何关于此存储文件的预期格式或其中应包含的内容的文档,或者如何以任何其他方式实例化凭证对象.对不起,我不能在这里展示任何作品,但我不知所措.任何正确方向的点都将非常感激.

oli*_*oil 9

非常简单,显然这是有效的:

from oauth2client.client import GoogleCredentials
from oauth2client import GOOGLE_TOKEN_URI

access_token = None
token_expiry = None
token_uri = GOOGLE_TOKEN_URI
user_agent = 'Python client library'
revoke_uri = None

gCreds = GoogleCredentials( 
    access_token, 
    client_id,
    client_secret, 
    refresh_token, 
    token_expiry,
    token_uri, 
    user_agent,
    revoke_uri=revoke_uri
    )
Run Code Online (Sandbox Code Playgroud)