遇到了找到适当范围以进行Google Drive API请求的问题。

huangapple go评论61阅读模式
英文:

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,如下所示:

遇到了找到适当范围以进行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 = &#39;fake-folder-url&#39;

load_dotenv()
api_key = os.getenv(&#39;JAKE_KEY&#39;)
json_key_location = os.getenv(&#39;API_KEY_JSON_PATH&#39;)
# Replace &#39;YOUR_JSON_FILE.json&#39; with the path to your downloaded JSON credentials file
credentials = service_account.Credentials.from_service_account_file(json_key_location, scopes=[&#39;https://www.googleapis.com/auth/documents.readonly&#39;])

# Create a Google Drive API service
drive_service = build(&#39;drive&#39;, &#39;v3&#39;, credentials=credentials)

folder_name = &#39;fake-folder-id&#39;

results = drive_service.files().list(q=f&quot;name = &#39;{folder_name}&#39; and mimeType = &#39;application/vnd.google-apps.folder&#39;&quot;).execute()
folders = results.get(&#39;files&#39;, [])

# Check if the folder was found and retrieve its ID
if folders:
    folder_id = folders[0][&#39;id&#39;]
    print(f&quot;The folder ID for &#39;{folder_name}&#39; is: {folder_id}&quot;)
else:
    print(f&quot;No folder found with the name &#39;{folder_name}&#39;&quot;)

This code is giving me this error

File &quot;get_txt.py&quot;, line 21, in &lt;module&gt;
    results = drive_service.files().list(q=f&quot;name = &#39;{folder_name}&#39; and mimeType = &#39;application/vnd.google-apps.folder&#39;&quot;).execute()
  File &quot;/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py&quot;, line 130, in positional_wrapper    return wrapped(*args, **kwargs)
  File &quot;/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/http.py&quot;, line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: &lt;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&amp;alt=json returned &quot;Request had insufficient authentication scopes.&quot;. Details: &quot;[{&#39;message&#39;: &#39;Insufficient Permission&#39;, &#39;domain&#39;: &#39;global&#39;, &#39;reason&#39;: &#39;insufficientPermissions&#39;}]&quot;&gt;

Which indicates a permissions issue. I have a json for the credentials of my project that has Google Drive API enabled here
遇到了找到适当范围以进行Google Drive API请求的问题。

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:

huangapple
  • 本文由 发表于 2023年5月25日 06:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定