簡體   English   中英

使用Python中的Storage JSON API列出Google存儲中某個項目下的所有存儲桶

[英]List all buckets under a project from the google storage using Storage JSON API in Python

我指的是下面的鏈接,在下面的鏈接中,我粘貼了代碼的副本。 https://developers.google.com/apis-explorer/?hl=zh_CN#p/storage/v1/storage.buckets.list

   import requests
   response = requests.get('https://www.googleapis.com/storage/v1/b?project=
   {YOUR_PROJECT_NAME}&key={YOUR_API_KEY}')
   print(response.json())

當我嘗試訪問它時,出現以下錯誤

{'error': {'errors': [{'domain': 'global', 'reason': 'required', 'message': 'Anonymous users does not have storage.buckets.list access to project 72
8242242638.', 'locationType': 'header', 'location': 'Authorization'}], 'code': 401, 'message': 'Anonymous users does not have storage.buckets.list a
ccess to project 728242242638.'}}

我缺少哪一部分?

您缺少身份驗證。 API密鑰可識別項目,但不提供任何身份驗證。 Google並不知道您確實是允許查看訪問令牌的用戶。

如果您希望命令真正快速gcloud並且已安裝gcloud SDK,請運行gcloud auth print-access-token 那會給你一個像ya29.blibbity_blabbity的字符串。 將標頭附加到您的請求中,顯示為Authorization: Bearer ya29.blibbity_blabbity ,您的請求應該會成功。

訪問令牌只能使用5分鍾左右,並且您不應該將它們硬編碼到代碼中。 您將需要一個更永久的解決方案。 最簡單的方法是只使用更高級別的Google Cloud庫,例如google-cloud ,但是如果您想繼續直接使用請求,則首先要了解更多Google Cloud身份驗證的詳細信息。 從這里開始: https : //cloud.google.com/docs/authentication

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM