google api(表格)请求的身份验证范围不足

ext*_*all 1 python google-api google-oauth google-api-python-client anaconda

我想从工作表中读取写入数据,读取工作正常,但写入却没有。我使用了文档中提到的所有范围:https//developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

data_writer(1,1,1)
Run Code Online (Sandbox Code Playgroud)

码:

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'+"https://www.googleapis.com/auth/drive.file"+"https://www.googleapis.com/auth/drive"
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = apiclient.discovery.build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
SPREADSHEET_ID = '1JwVOqtUCWBMm_O6esIb-9J4TgqAmMIdYm9sf5y-A7EM'
RANGE_NAME = 'Jokes!A:C'
# How the input data should be interpreted.
value_input_option = 'USER_ENTERED'  
# How the input data should be inserted.
insert_data_option = 'INSERT_ROWS' 

def data_reader():
    #reading data
    read = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,range=RANGE_NAME).execute()
    #reading values
    values = read.get('values', [])
    if not values:
        print('No data found.')
    else:

        for row in values:
            print(row[2])
            continue
def data_writer(score,num_comments,mystring):
    value_range_body = {
        "score":score,
        "num_comments":num_comments,
        "joke":mystring
    }
    request = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
    response = request.execute()
Run Code Online (Sandbox Code Playgroud)

ska*_*mit 6

SCOPES必须为列表类型

SCOPES = [' https://www.googleapis.com/auth/spreadsheets ',“ https://www.googleapis.com/auth/drive.file ”,“ https://www.googleapis.com/auth/开车 “]

旁注:你有

... /验证/驱动器

... /认证/驱动器。文件

/drive.file限制了该API仅与drive.file API一起使用,但是/ drive打开了所有驱动器API。因此,您应该选择一种适合您的需求。

旁注2:根据您提供的链接,它提到您需要至少一个API才能与电子表格一起使用,因此也可能不需要全部。

  • 他们必须查看其文档! (2认同)