Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets Google API token.json文件是一次性使用的吗?_Google Sheets_Oauth 2.0_Google Api_Google Oauth_Google Sheets Api - Fatal编程技术网

Google sheets Google API token.json文件是一次性使用的吗?

Google sheets Google API token.json文件是一次性使用的吗?,google-sheets,oauth-2.0,google-api,google-oauth,google-sheets-api,Google Sheets,Oauth 2.0,Google Api,Google Oauth,Google Sheets Api,我正在制作一个简单的应用程序来访问我保存在谷歌硬盘上的谷歌表单。我在google上建立了一个项目,创建了Oauth凭据,并运行Python快速启动代码来生成token.json文件 昨天,在这样做之后,我运行了这部分快速启动代码,它运行得非常完美,并从示例电子表格返回了行: ###Add step to pull in previous staff comments, joino in MRN from __future__ import print_function from googleap

我正在制作一个简单的应用程序来访问我保存在谷歌硬盘上的谷歌表单。我在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中重新验证并创建一个新的令牌文件


谢谢

token.json是单用户的,不应单次使用。您是否可以尝试删除或移动token.json,然后看看它是否有效?你真的不需要床单和驱动范围。驱动器作用域将允许您访问用户驱动器帐户上的所有内容工作表作用域允许您访问他们的所有工作表,请坚持使用您真正需要的工作表need@DalmTo我打开了令牌文件,它看起来有一个“到期”部分,日期和时间是昨天晚上9点,所以它看起来确实到期了。我不得不假设这就是问题所在。你知道一种没有这个问题的方法吗?@DalmTo还有-我想问一下你提到的范围问题。我重新运行了quickstart代码,现在它开始工作了。但是,只有当我使用https:www.googleapis.com/auth/spreadsheets.readonly“范围时,它才起作用。但是我不希望它是只读的,我希望能够写,但是当我使用没有“.readonly”的电子表格范围时,表示作用域无效。你知道为什么吗?你不能加上这个?确保删除令牌文件,以便它强制您重新授权代码