"This site can’t be reached. localhost refused to connect." 在 google 张 api 尝试授权后

"This site can’t be reached. localhost refused to connect." After authorization attempt from google sheets api

我对 python 和一般的编程还很陌生,我认为通过对我的 google sheet。我按照 google 的 python sheets api 快速入门使用 repl.it 然后使用 JupyterLab(请参阅 post 底部的代码).在获得授权之前,一切都很好。当我点击 link 它发送给我并选中“允许”时,chrome 给了我错误:“无法访问此站点。localhost 拒绝连接”站点:http:// localhost:49471/.

(让我感到困惑的第一件事是,我认为 repl.it 没有与我计算机本地的任何内容进行交互,而是在云中进行所有操作。[=85= 可能出了问题] 还是我不明白 localhost 是什么意思?)

我正在使用 mac,无论我尝试过哪种浏览器,都会发生这种情况:chrome、safari、firefox)。

这可能看起来像是另一个 Whosebug 问题的重复,但我已经尝试了我在所有其他看起来相关的问题上找到的建议,并且 none 对我有用(google doc api - localhost refusing to connect, , , ).我尝试过的事情包括:

  1. 在 google 云中,我创建了一个采用 HTTP 引荐来源网址的 API 密钥:

localhost
localhost:*
*localhost*
http://localhost:49471
localhost:49471

  1. 在 google 云中,我创建了一个 API 代码,它获取 IP 地址并上传了我自己的 IP 地址。

  2. 在 google 云中我创建了一个 OAuth 2.0 客户端 ID 网络应用程序(它可以访问 google sheet 因为它与互联网上的每个人共享: https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit#gid=0)

  3. 在我的 mac 系统偏好设置中,我暂时允许传入连接通过防火墙进入 chrome

  4. 我检查以确保成为该项目测试用户的帐户与尝试访问它的用户相同。

  5. 在我的 mac 系统偏好设置中,我告诉它绕过 localhost:49471

    的代理设置
  6. 我尝试重新启动 mac 并重新启动浏览器

  7. 在代码中添加DriveApp.getStorageLimit();

  8. 关闭防火墙并重新启动计算机

  9. 使用 JupyterLab 而不是 Replit

  10. 正在隐身打开授权行window

编辑:我在最初 posting 三天后回顾这件事。我已经尝试了上面写的每个步骤,但仍然无法正常工作。如果我能做些什么来改进这个问题,请告诉我,因为我看到很多人都看过它,但还没有人回答。非常感谢您提供的任何帮助。开始这个项目会很好!

这是我从 google sheets api python 快速入门中复制粘贴的代码:

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

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

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'

def main():
    """Shows basic usage of the Sheets API.
    Prints values from a sample spreadsheet.
    """
    creds = None
    # The file token.json 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.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # 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.json', 'w') as token:
            token.write(creds.to_json())

    service = build('sheets', 'v4', credentials=creds)

    # Call the Sheets API
    sheet = service.spreadsheets()
    result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                range=SAMPLE_RANGE_NAME).execute()
    values = result.get('values', [])

    if not values:
        print('No data found.')
    else:
        print('Name, Major:')
        for row in values:
            # Print columns A and E, which correspond to indices 0 and 4.
            print('%s, %s' % (row[0], row[4]))

if __name__ == '__main__':
    main()

据我所知,Repl.it 和 Jupyter 都不允许这种传入连接。我现在直接从我的计算机上处​​理我的应用程序(并收到一种新的错误)。如果有人看到这个并认为这是错误的,请告诉我。另外,如果有人知道可以放置允许 google 连接的代码的在线位置,请告诉我。否则,我会在 Whosebug 允许时将其标记为已回答。