Google Media API代码401请求在MediaItem获取期间缺少必需的身份验证凭据

时间:2019-05-07 03:11:22

标签: python-3.x authentication oauth-2.0 google-oauth

我有一些python代码可以成功验证并从google photos api中提取各种对象。

from googleapiclient.discovery import build


def build_service(credentials):
    API_SERVICE_NAME = 'photoslibrary'
    API_VERSION = 'v1'

    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)

def get_albums(service):
    results = service.albums().list().execute()
    return results.get('albums', [])

def get_pictures(service,album_id: str):
    body = {
        "albumId": album_id,
        "page_size": 10
    }
    print(body)
    results = service.mediaItems().search(body=body).execute()
    return results

def get_picture_url(service,media_item_id: str):
    return service.mediaItems().get(mediaItemId=media_item_id)

除get_picture_url之外的所有功能均起作用,这导致以下响应。通过外壳工作,我可以使用list方法成功列出mediaItems,但无法通过get()方法访问它们。

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

编辑:我的范围设置为只读。

0 个答案:

没有答案