【问题标题】:google api (sheets) Request had insufficient authentication scopesgoogle api (sheets) 请求的身份验证范围不足
【发布时间】:2018-07-15 15:26:23
【问题描述】:

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

data_writer(1,1,1)

代码:

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()

【问题讨论】:

    标签: python google-api anaconda google-oauth google-api-python-client


    【解决方案1】:

    SCOPES 必须是列表类型

    范围 = ['https://www.googleapis.com/auth/spreadsheets', "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]

    旁注:你有

    .../auth/驱动器

    .../auth/drive.file

    /drive.file 将 API 限制为仅使用 drive.file API,但 /drive 对所有驱动器 API 开放。所以你应该选择一个适合你的需求。

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

    【讨论】:

    【解决方案2】:

    首先,您应该只需要https://www.googleapis.com/auth/drive,因为它提供了对用户驱动器帐户的完全访问权限,包括阅读和书写工作表。

    如果您已经运行过一次代码并验证了您的用户,则更改代码中的范围。请记住,您将需要再次运行代码并重新验证用户身份以获得新范围授予的访问权限。

    旁注:

    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'+"https://www.googleapis.com/auth/drive.file"+"https://www.googleapis.com/auth/drive"
    

    只是一个长字符串,您需要用空格分隔它们,或者其他答案状态使用数组。

    SCOPES = 'https://www.googleapis.com/auth/spreadsheets ' + "https://www.googleapis.com/auth/drive.file " + "https://www.googleapis.com/auth/drive"
    

    【讨论】:

    • 所以我在 console.developers.google.com 中激活了 google drive api,这就是我需要做的所有验证。我更改了这一行:SCOPES = "googleapis.com/auth/drive"
    • 是的,这应该是您设置客户端所需要做的所有事情。当您运行代码时,您的用户需要使用他们的登录名和密码登录并同意您访问他们的谷歌驱动器数据。
    • 它不要求登录,所以我想我同意了。但它仍然不起作用,它给出了相同的错误消息
    • 它可能已经有一次你现在需要重置它 checkout store = file.Storage('credentials.json')
    • # 如果修改这些范围,删除文件token.pickle。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2016-06-21
    • 2016-04-28
    • 1970-01-01
    • 2021-12-12
    • 2023-03-13
    相关资源
    最近更新 更多