google-sheets - Google API token.json 文件是一次性使用的吗?
问题描述
我正在制作一个简单的应用程序来访问我保存在谷歌驱动器上的谷歌表格。我在 google 上建立了一个项目,创建了 Oauth 凭据并运行 Python 快速入门代码以生成 token.json 文件。
昨天,在这样做之后,我运行了这部分快速入门代码,它运行完美并返回了示例电子表格中的行:
###Add step to pull in previous staff comments, joino in MRN
from __future__ import print_function
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
###This only runs when not connected to NetExtender. Should work when ported to citrix but running locally for testing is going to be difficult
###Gsheets
SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
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]))
但是,今天当我运行该代码时,它不再起作用了。我收到一个错误:
('invalid_scope: Bad Request', {'error': 'invalid_scope', 'error_description':'Bad Request'})
令牌文件是否一次性使用,每次我想运行它时是否需要生成一个新的(这是我能想象它昨天可以正常工作但今天不行的唯一原因)?如果这是问题所在,有没有办法对此进行编程,这样我就不需要在每次运行时都在 Google 中重新进行身份验证并创建一个新的令牌文件?
谢谢!
解决方案
推荐阅读
- html - 表格未正确插入堵塞
- c# - 如何从孩子中的孩子访问列表?
- python - 为什么 Buildozer 在将 main.py 转换为 apk 时会出现此错误
- javascript - 数据导致 Ajax POST 调用在客户端失败
- dialogflow-es - How to replace an existing DialogFlow action with Assistant SDK action
- sql-server - Error handler for insert into before deleting the records
- bash - can I get the object file id for a deleted file with git?
- python-3.x - Why does ResNet models have consecutive BatchNormalization layers?
- javascript - How to make array wait for nested loops to complete before returning
- vue.js - Vuetify align list to right side of parent card