3 回答

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
有一個(gè)如何使用InstalledAppFlow將 Google Docs 與 python 一起使用的示例:
from google_auth_oauthlib.flow import InstalledAppFlow

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
您將希望使用已安裝的應(yīng)用程序流而不是 Web 應(yīng)用程序流。
原因是 Colab 后端沒(méi)有直接暴露給 Internet,因此無(wú)法處理基于 Web 的 OAuth 流常見(jiàn)的 URL 重定向。

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
按照評(píng)論和本教程(您可以從中受益以在控制臺(tái)中創(chuàng)建應(yīng)用程序),我實(shí)現(xiàn)了以下代碼:
creds = None
# Check if previous log-in exists
if os.path.exists('/content/drive/MyDrive/Projects/xyz/token.pickle'):
with open('/content/drive/MyDrive/Projects/xyz/token.pickle', 'rb') as token:
creds = pickle.load(token)
# If not available or invalid, log in using the "console" InstalledAppFlow
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('/content/drive/MyDrive/Projects/xyz/credentials.json', SCOPES)
creds = flow.run_console()
# Save the access token in token.pickle file for the next run
with open('/content/drive/MyDrive/Projects/xyz/token.pickle', 'wb') as token:
pickle.dump(creds, token)
# Connect to the Gmail API
service = build('gmail', 'v1', credentials=creds)
添加回答
舉報(bào)