【问题标题】:Why do I get an invalid scope error using gspread为什么使用 gspread 会出现无效范围错误
【发布时间】:2020-03-26 09:06:37
【问题描述】:
import discord
from discord.ext import commands
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint

scope = ["""https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/sprea...,"https://www.googleapis.com/auth/drive...","https://www.googleapis.com/auth/drive"""]

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)

client = gspread.authorize(creds)

sheet = client.open("NYRP Clock Ins").sheet1  # Open the spreadhseet

data = sheet.get_all_records()  # Get a list of all records

row = sheet.row_values(3)  # Get a specific row
col = sheet.col_values(3)  # Get a specific column
cell = sheet.cell(1,2).value  # Get the value of a specific cell

insertRow = ["hello", 5, "red", "blue"]
sheet.add_rows(insertRow, 4)  # Insert the list as a row at index 4

sheet.update_cell(2,2, "CHANGED")  # Update one cell

numRows = sheet.row_count  # Get the number of rows in the sheet

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')

    async def on_message(self, message):
        # we do not want the bot to reply to itself
        if message.author.id == self.user.id:
            return

        if message.content.startswith('.clock in'):
            embed = discord.Embed(title='BCSO', description='Succesfully Clocked In', color=0x5ed623)
            embed.add_field(name='.clock out', value='Clocks out')
            await message.channel.send(content=None, embed=embed,)
            insertRow = ["hello", 5, "red", "blue"]
            sheet.add_rows(insertRow, 4)  # Insert the list as a row at index 4


        if message.content.startswith('.clock out'):
            embed = discord.Embed(title='BCSO', description='Succesfully Clocked Out', color=0xef210a)
            embed.add_field(name='.clock in', value='Clocks In')
            await message.channel.send(content=None, embed=embed,)

无效范围:https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/sprea...,"https://www.googleapis.com/auth/drive...","https://www.googleapis.com/auth /drive 不是有效的受众字符串。

当我尝试运行机器人但找不到修复程序时收到上述错误消息

【问题讨论】:

  • 我使用 gspread 文档中使用的代码修复了解决方案:``` import gspread from oauth2client.service_account import ServiceAccountCredentials scope = ['spreadsheets.google.com/feeds', 'googleapis.com/auth/drive'] credentials = ServiceAccountCredentials. from_json_keyfile_name('(creds.json 或任何你命名的 json 文件)', scope) ```

标签: python python-3.x google-apps-script google-sheets gspread


【解决方案1】:

您使用的范围没有以正确的方式定义。

你可以试试这个:

scope = ["https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.appfolder",
"https://spreadsheets.google.com/feeds"]

有关范围的更多信息,您可以尝试阅读以下文档。

[1]https://developers.google.com/drive/api/v2/about-auth

[2]https://developers.google.com/sheets/api/guides/authorizing

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2020-02-29
    • 2017-12-02
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多