我正在尝试创建一个不和谐的机器人,当给出命令时,它将编辑谷歌表格中的指定列和行。 我配置了谷歌云应用程序,因此它具有对谷歌表格文件的所有者访问权限,但是当我尝试运行机器人时,它显示一个错误,指出它的身份验证范围不足。 我应该更改哪些设置? 还有更好的机器人编码方法吗?
这是代码:
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'}}]}
产生错误的方法是
google.apps.drive.v3.DriveFiles.List
。这意味着您的应用没有列出 Google 云端硬盘文件的权限。
添加以下范围:
https://www.googleapis.com/auth/drive