API错误:请求的认证范围不足

0 投票
1 回答
31 浏览
提问于 2025-04-14 17:38

我正在尝试创建一个Discord机器人,当我输入特定命令时,它会编辑Google表格中的指定列和行。我已经配置了Google云应用,使其拥有对Google表格文件的所有者访问权限,但当我尝试运行这个机器人时,出现了一个错误,提示说它的认证范围不足。我应该更改哪些设置?另外,还有没有更好的方法来编写这个机器人?

这是代码:

import discord
from discord.ext import commands
import gspread
from oauth2client.service_account import ServiceAccountCredentials

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix='/', intents=intents)
@bot.event
async def on_ready():
    print('Logged in as {0.user}'.format(bot))

scope = ['https://www.googleapis.com/auth/spreadsheets']
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)

client = gspread.authorize(creds)
sheet = client.open('Kopia The LOST MC').sheet1


@bot.command()
async def skladka(ctx, user: discord.Member):
    discord_id = str(user.id)
    cell_with_discord_id = sheet.find(discord_id)

    value_to_copy = sheet.cell(116, 2).value

    sheet.update_cell(cell_with_discord_id.row, 4, value_to_copy)

bot.run('token')

这是错误信息:

Traceback (most recent call last):
  File "c:\Users\szymo\Desktop\discord-lost\bot.py", line 18, in <module>
    sheet = client.open('Kopia The LOST MC').sheet1
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\szymo\AppData\Local\Programs\Python\Python312\Lib\site-packages\gspread\client.py", line 123, in open
    spreadsheet_files, response = self._list_spreadsheet_files(title, folder_id)
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\szymo\AppData\Local\Programs\Python\Python312\Lib\site-packages\gspread\client.py", line 96, in _list_spreadsheet_files
    response = self.http_client.request("get", url, params=params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\szymo\AppData\Local\Programs\Python\Python312\Lib\site-packages\gspread\http_client.py", line 123, in request
    raise APIError(response)
gspread.exceptions.APIError: {'code': 403, 'message': 'Request had insufficient authentication scopes.', 'errors': [{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}], 'status': 'PERMISSION_DENIED', 'details': [{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'service': 'drive.googleapis.com', 'method': 'google.apps.drive.v3.DriveFiles.List'}}]}

1 个回答

0

出现错误的方法是 google.apps.drive.v3.DriveFiles.List。这意味着你的应用没有权限去查看 Google Drive 上的文件。

你需要添加以下权限范围:

https://www.googleapis.com/auth/drive

撰写回答