gpt4 book ai didi

python - 如何从备用源创建 Google API OAuth Credentials 对象

转载 作者:太空狗 更新时间:2023-10-29 21:34:20 25 4
gpt4 key购买 nike

我正在使用这个简单的 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)

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

问题是我找不到任何关于此存储文件的预期格式的文档,或者它应该包含什么,或者如何以任何其他方式实例化凭据对象。抱歉,我不能在这里展示任何作品,但我很茫然。任何指向正确方向的观点都将不胜感激。

最佳答案

很简单,显然这是可行的:

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
)

关于python - 如何从备用源创建 Google API OAuth Credentials 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26805866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com