为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Oauth2 登录 Google Colab

使用 Oauth2 登录 Google Colab

侃侃尔雅 2022-01-18 17:37:37
我正在尝试使用 Oauth 2.0 登录 Google Colab 笔记本,但启动的 Google 登录网页以不存在的 web 结束(http://localhost:8090/?code=4/NAFDWUxkOxp3FIlB9I_vxFTFm-zjAx0XJpEspQ9dGozG-0L3ccthFD12FAhq_B5hLWTxDFuUg_SjC011V9jiLDw&scope=https://www.googleapis.com/auth/admin.directory.group.member%20https://www.googleapis.com/auth/admin.directory.group%20https://www.googleapis.com/auth/apps.groups.settings)。也许该错误与任何回调 url 有关,但我不知道如何解决。这是我用于登录的代码。import osos.environ['USE_NATIVE_IPYTHON_SYSTEM_COMMANDS'] = '1'import httplib2from oauth2client.client import flow_from_clientsecretsfrom oauth2client.file import Storagefrom oauth2client.tools import run_flowfrom oauth2client import toolsfrom google.colab import drive; drive.mount('/content/drive')PATH_BASE = '/content/drive/My Drive/.../'CLIENT_SECRET = PATH_BASE + 'client_secret_event.json'SCOPES = [ "https://www.googleapis.com/auth/admin.directory.group", "https://www.googleapis.com/auth/admin.directory.group.member", "https://www.googleapis.com/auth/apps.groups.settings"]STORAGE = Storage(PATH_BASE + 'credentials.storage')flags = tools.argparser.parse_args([])# Start the OAuth flow to retrieve credentialsdef authorize_credentials():    # Fetch credentials from storage    credentials = STORAGE.get()    # If the credentials doesn't exist in the storage location then run the flow    if credentials is None or credentials.invalid:        flow = flow_from_clientsecrets(CLIENT_SECRET, scope=SCOPES)        http = httplib2.Http()        credentials = run_flow(flow, STORAGE, flags, http=http)    return credentialscredentials = authorize_credentials()关于为什么会发生这种情况的任何想法?
查看完整描述

3 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

有一个如何使用InstalledAppFlow将 Google Docs 与 python 一起使用的示例:

from google_auth_oauthlib.flow import InstalledAppFlow


查看完整回答
反对 回复 2022-01-18
?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

您将希望使用已安装的应用程序流而不是 Web 应用程序流。

原因是 Colab 后端没有直接暴露给 Internet,因此无法处理基于 Web 的 OAuth 流常见的 URL 重定向。


查看完整回答
反对 回复 2022-01-18
?
慕斯王

TA贡献1864条经验 获得超2个赞

按照评论和本教程(您可以从中受益以在控制台中创建应用程序),我实现了以下代码:


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)


查看完整回答
反对 回复 2022-01-18
  • 3 回答
  • 0 关注
  • 435 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号