【问题标题】:Google Drive API OAuth 2.0: ERR_CONNECTION_REFUSEDGoogle Drive API OAuth 2.0:ERR_CONNECTION_REFUSED
【发布时间】:2020-04-07 17:06:36
【问题描述】:

我在我的个人 GCP 中启用了 Google Drive API,设置了我的 OAuth2 同意屏幕,并下载了我的 credentials.json 文件。我现在从 Colab 中的 Python quickstart 运行这段代码:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
    main()

当我执行代码时,系统会提示我通过 Google 的 OAuth 登录页面登录,以便让我的程序访问我的 Google 帐户。我点击允许,然后立即看到这个页面:

我在没有 VPN 或防火墙的情况下重做此操作,但没有运气,我不知道这是否是我的 OAuth 或项目中的凭据设置有问题。想法?

【问题讨论】:

  • 尝试从creds = flow.run_local_server(port=0)中删除端口参数默认端口是8080,所以如果你已经有东西在使用该端口,尝试一个大于1023小于65535的数字。端口号0无效。
  • 我尝试了 1023 和 65535 之间的各种端口,使用端口 8080,也没有端口参数。不幸的是没有运气:(但如果它是错误的根源,我可以更深入地研究。
  • run_local_server() 启动本地网络服务器。出了点问题,服务器没有启动(AFAIK)。您是否看到任何错误消息等?使用有关如何运行代码等的更多详细信息编辑您的问题。
  • 我通过在 PyCharm 而不是 Colab 中本地运行它来修复错误。
  • 我建议您使用找到的解决方案发布答案​​。

标签: python google-cloud-platform google-drive-api google-oauth


【解决方案1】:

通过在 PyCharm 中运行解决。 VSCode 也可以。无论出于何种原因,Colab 都会继续抛出错误。

【讨论】:

    【解决方案2】:

    提供解决方案:

    run_local_server()打开浏览器时,复制url并使用隐私浏览通过此链接登录。

    它对我有用。

    【讨论】:

    • 欢迎利尔!很高兴有你在这里。一些东西。如果您查看其他答案,您可以看到原始发帖人回答了他自己的问题。考虑在答案中添加更多细节,特别是如果它们是旧的。此外,您提到了一个链接,但没有,您可以编辑您的答案并根据需要添加一个。 Stackoverflow 上有一些礼仪标准,你可以在这里找到它们stackoverflow.com/conduct
    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多