【问题标题】:How can I create an album through Google Photos API with google-api-python-client?如何使用 google-api-python-client 通过 Google Photos API 创建相册?
【发布时间】:2021-10-11 08:07:20
【问题描述】:

我采用了这个code,它只需稍加修改即可工作(添加static_discovery=False):

service = build('photoslibrary', 'v1', credentials = creds, static_discovery=False)

但这只会读取数据。 我想创建一个相册。 所以,我补充说:

response_create_album = service.albums().create(body={'album':{'title':'helloworld'}}).execute()

给了我这个消息没有用:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://photoslibrary.googleapis.com/v1/albums?alt=json returned "Request had insufficient authentication scopes."

我使用了不同的范围:

'https://www.googleapis.com/auth/photoslibrary.readonly'
'https://www.googleapis.com/auth/photoslibrary.edit'
'https://www.googleapis.com/auth/photoslibrary.edit.appcreateddata'
'https://www.googleapis.com/auth/photoslibrary.appendonly'

没有任何效果。

我最终怎样才能成功?

【问题讨论】:

    标签: python-3.x google-api-python-client google-developers-console google-photos-api


    【解决方案1】:

    这是一个直接使用 REST API 而不是 REST 包装器之一的解决方案:

    _header = {'Authorization': 'Bearer {}'.format(_credentials.token), 'Content-Type': 'application/json'}
    _google_albums_url = 'https://photoslibrary.googleapis.com/v1/albums'
    _body = {"album": {'title': 'my-album'}}
    _response = requests.post(_google_albums_url, headers=_header, data=str(_body))
    

    【讨论】:

    • 为什么选择原始 REST?因为我发现包装器 a) 有时有点不稳定,尤其是在超时时,b) 它们经常更改 c) 包装器并不总是具有我需要的功能,所以我最终得到了服务包装器和 REST 的混搭。最后,我只是将全部转换为 REST 调用,我的经验是一个更稳定的应用程序,所需的更改更少。 YMMV。
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多