【发布时间】:2021-06-23 15:21:23
【问题描述】:
我对 python 和一般编程非常陌生,我认为通过编写我的谷歌表来学习我的新 python 技能会很好。我使用 repl.it 遵循了谷歌的 python sheet api quickstart,然后使用了 JupyterLab(代码见文章底部)。在获得授权之前,一切都运行良好。当我点击它发送给我的链接并选中“允许”时,chrome 给我一个错误:“无法访问此站点。本地主机拒绝连接”站点:http://localhost:49471/。
(首先让我感到困惑的是,我认为 repl.it 没有与我的计算机本地的任何东西进行交互,而是在云中做所有事情。repl.it 可能出了问题还是我不明白localhost 是什么意思?)
我使用的是 Mac,无论我尝试过哪种浏览器:chrome、safari、firefox,都会发生这种情况。
这可能看起来像另一个 stackoverflow 问题的重复,但我已经尝试了我在所有其他看起来相关的问题上找到的建议,但没有一个对我有用(google doc api - localhost refusing to connect、Access Google Drive from Sheets: authorization、@987654323 @,Accessing Google Sheets API from Google Compute Engine (python))。我尝试过的事情包括:
- 在谷歌云中,我创建了一个接受 HTTP 引用的 API 密钥:
localhost
localhost:*
*localhost*
http://localhost:49471
localhost:49471
-
在谷歌云中,我创建了一个获取 IP 地址并上传我自己的 IP 地址的 API 代码。
-
在谷歌云中,我创建了一个 OAuth 2.0 客户端 ID 网络应用程序(它可以访问谷歌表,因为它与互联网上的所有人共享:https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit#gid=0)
-
在我的 mac 系统偏好设置中,我暂时允许传入连接通过 chrome 的防火墙
-
我检查以确保成为项目测试用户的帐户与尝试访问它的用户相同。
-
在我的 mac 系统偏好设置中,我告诉它绕过 localhost:49471 的代理设置
-
我尝试重新启动我的 mac 并重新启动浏览器
-
在代码中添加
DriveApp.getStorageLimit(); -
关闭我的防火墙并重新启动我的计算机
-
使用 JupyterLab 代替 Replit
-
在隐身窗口中打开授权行
编辑:我在最初发布三天后回头看这个。我已经再次尝试了上面写的每个步骤,但仍然无法正常工作。如果我能做些什么来改善这个问题,请告诉我,因为我看到很多人都看过它,但还没有人回答。非常感谢您提供的任何帮助。很高兴开始这个项目!
这是我从 google sheet api python quickstart 复制并粘贴的代码:
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()
【问题讨论】:
标签: python google-apps-script google-api google-oauth