英文:
Having Trouble finding the correct scope for my API request to Google Drive API
问题
以下是代码的翻译部分:
这很可能只是我不知道如何找到正确的范围,但我仍然需要帮助。
from google.oauth2 import service_account
from googleapiclient.discovery import build
import requests
import pprint
from dotenv import load_dotenv
import os
forcewinds_folder_url = '假文件夹网址';
load_dotenv()
api_key = os.getenv('JAKE_KEY')
json_key_location = os.getenv('API_KEY_JSON_PATH')
# 用您下载的 JSON 凭据文件的路径替换 'YOUR_JSON_FILE.json'
credentials = service_account.Credentials.from_service_account_file(json_key_location, scopes=['https://www.googleapis.com/auth/documents.readonly'])
# 创建一个 Google Drive API 服务
drive_service = build('drive', 'v3', credentials=credentials)
folder_name = '假文件夹ID'
results = drive_service.files().list(q=f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder'").execute()
folders = results.get('files', [])
# 检查是否找到文件夹并检索其 ID
if folders:
folder_id = folders[0]['id']
print(f"'{folder_name}' 的文件夹 ID 为:{folder_id}")
else:
print(f"未找到名称为 '{folder_name}' 的文件夹")
此代码给我返回以下错误:
File "get_txt.py", line 21, in <module>
results = drive_service.files().list(q=f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder'").execute()
File "/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?q=name+%3D+%271RyT2IS-dhGLCNcod_LVt246463PBR3bH%27+and+mimeType+%3D+%27application%2Fvnd.google-apps.folder%27&alt=json returned "Request had insufficient authentication scopes.". Details: [{"message": "Insufficient Permission", "domain": "global", "reason": "insufficientPermissions"}]>
这表明存在权限问题。我有一个用于我的项目凭据的 JSON,其中启用了 Google Drive API,如下所示:
我确保每次向该帐户添加权限时都更新了我的身份验证 JSON,并且已经给予了访问我的帐户上的 Google Drive 文件夹所需的一切权限,但我仍然感到困惑。我相当确定我在进行身份验证和创建凭据时是正确的。我在这里做错了什么?
英文:
This is likely just a matter of me not knowing how to find the correct scope but I need help all the same.
from google.oauth2 import service_account
from googleapiclient.discovery import build
import requests
import pprint
from dotenv import load_dotenv
import os
forcewinds_folder_url = 'fake-folder-url'
load_dotenv()
api_key = os.getenv('JAKE_KEY')
json_key_location = os.getenv('API_KEY_JSON_PATH')
# Replace 'YOUR_JSON_FILE.json' with the path to your downloaded JSON credentials file
credentials = service_account.Credentials.from_service_account_file(json_key_location, scopes=['https://www.googleapis.com/auth/documents.readonly'])
# Create a Google Drive API service
drive_service = build('drive', 'v3', credentials=credentials)
folder_name = 'fake-folder-id'
results = drive_service.files().list(q=f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder'").execute()
folders = results.get('files', [])
# Check if the folder was found and retrieve its ID
if folders:
folder_id = folders[0]['id']
print(f"The folder ID for '{folder_name}' is: {folder_id}")
else:
print(f"No folder found with the name '{folder_name}'")
This code is giving me this error
File "get_txt.py", line 21, in <module>
results = drive_service.files().list(q=f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder'").execute()
File "/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs)
File "/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?q=name+%3D+%271RyT2IS-dhGLCNcod_LVt246463PBR3bH%27+and+mimeType+%3D+%27application%2Fvnd.google-apps.folder%27&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]">
Which indicates a permissions issue. I have a json for the credentials of my project that has Google Drive API enabled here
I've made sure I've updated my authentication json every time I add permissions to that account and I've given it everything that seems to make sense to me to access a google drive folder on my account. Im fairly sure that I am authenticating and creating my credentials correctly so im fairly stuck.
What am I doing wrong here?
答案1
得分: 2
在你的脚本中,似乎使用了https://www.googleapis.com/auth/documents.readonly
作为范围。但是,在你的脚本中,你试图使用Drive API。我认为这可能是你当前遇到Request had insufficient authentication scopes.
问题的原因。在这种情况下,请修改范围。请从以下范围中选择你想要使用的范围。
如果你只使用Drive API v3的“Method: files.list”,你可以使用以下范围。
https://www.googleapis.com/auth/drive.metadata.readonly
或者,如果你想要检索文件内容,请使用以下范围。
https://www.googleapis.com/auth/drive.readonly
或者,如果你想要检索并更新文件,请使用以下范围。
https://www.googleapis.com/auth/drive
英文:
It seems that in your showing script, https://www.googleapis.com/auth/documents.readonly
is used as the scope. But, in your script, you are trying to use Drive API. I thought that this might be the reason for your current issue of Request had insufficient authentication scopes.
. In this case, please modify the scope. Please select the scope you want to use from the following scopes.
If you use only "Method: files.list of Drive API v3", you can use the following scope.
https://www.googleapis.com/auth/drive.metadata.readonly
or, if you want to retrieve the file content, please use the following scope.
https://www.googleapis.com/auth/drive.readonly
or, if you want to retrieve and update the files, please use the following scope.
https://www.googleapis.com/auth/drive
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论