我有一個很艱難的時期讓應用程序的默認Credientials /OAuth2.0的事情在我的計劃工作...谷歌雲存儲訪問客戶端API
背景:
我有一個傳感器裝置收集圖像,我想將這些圖像存儲在谷歌雲存儲中。理想情況下,代碼只需按下一個按鈕即可運行,因此在程序運行時需要執行授權。
從閱讀和重新閱讀的文檔,這是我迄今打算:
from oauth2client.client import GoogleCredentials
from google.cloud import storage
import google.auth
import os
# Establish cloud credientials with a locally
# stored service account key
home_path = os.path.expanduser('~') + "/Project/"
auth_file = "<service account access key file>.json"
auth_file_path = home_path + auth_file
# I'm trying two different ways to verify my credentials..
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = auth_file_path
credentials, project_id = google.auth.default()
# Get storage bucket for image files
storage = storage.Client(project_id)
bucket = storage.lookup_bucket('bucket0000')
其結果是,我得到一個回溯錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 110, in <module>
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/client.py", line 193, in lookup_bucket
return self.get_bucket(bucket_name)
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/client.py", line 173, in get_bucket
bucket.reload(client=self)
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/_helpers.py", line 99, in reload
_target_object=self)
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/_http.py", line 303, in api_request
error_info=method + ' ' + url)
google.cloud.exceptions.Forbidden: 403 Caller does not have storage.buckets.get access to bucket bucket0000. (GET https://www.googleapis.com/storage/v1/b/bucket0000?projection=noAcl)
地方似乎我仍被視爲匿名用戶(根據追蹤中的googleapis.com鏈接)
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Anonymous users does not have storage.buckets.get access to bucket bucket0000.",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Anonymous users does not have storage.buckets.get access to bucket bucket0000."
}
}
再次嘗試,這次google.auth,我創建了一個證書變量,並觀察什麼打印似乎有效:<oauth2client.service_account._JWTAccessCredentials object at 0x107df0358>
我試圖把這個憑證變量爲storage.Client(),但似乎並不上班。
# Get storage bucket for image files
storage = storage.Client(project_id, credentials=credentials)
bucket = storage.lookup_bucket('bucket0000')
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 6, in <module>
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/storage/client.py", line 59, in __init__
_http=_http)
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/client.py", line 212, in __init__
Client.__init__(self, credentials=credentials, _http=_http)
File "/Users/joshua/anaconda/lib/python3.5/site-packages/google/cloud/client.py", line 126, in __init__
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
ValueError: This library only supports credentials from google-auth-library-python. See https://google-cloud-python.readthedocs.io/en/latest/google-cloud-auth.html for help on authentication with this library
當然,這回溯指導我的頁面google-cloud-python.readthedocs.io ......不存在......
任何方式來獲得API調用到雲存儲在沒有使用控制檯/網絡登錄的情況下工作?
非常感謝!
您真的在使用Google App Engine嗎?從代碼中不清楚。針對應用引擎和其他應用的雲端存儲身份驗證有所不同,因此務必要清楚這一點。 – snakecharmerb