使用google-api-python-client使用Python访问Google Photo API [英] Access Google Photo API with Python using google-api-python-client

查看:317
本文介绍了使用google-api-python-client使用Python访问Google Photo API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Google API客户端库对其进行分页可以使用python客户端库访问Google Photos API,但是使用pip install -t lib/ google-api-python-client安装Google客户端库后,我看不到与Photos API相关的任何内容.

According to Google API Client Libraries page it is possible to access the Google Photos API using the python client library, but after installing it using pip install -t lib/ google-api-python-client I don't see anything related to Photos API.

如何使用Google构建的客户端库,而不是手动调用REST API?

How can I use the Google built client library instead of manually calling the REST APIs?

推荐答案

感谢 brillb 的示例,我终于也解决了我的问题.上面给出的某些文档链接不再有效.为了增强上述示例,我发现了页面 Google Photos API 最有用.它不仅记录了API,而且还允许您以交互方式测试您的请求-如果没有该测试功能,我可能永远无法使它正常工作.输入请求后,您可以在cURL,HTTP或JAVASCRIPT中看到您的编码示例-但对于Python没有任何显示.

Thanks to Ido Ran's and brillb's examples, I finally got my problem solved too. Some of the documentation links given above are not valid any more. Trying to enhance above examples, I found the page Google Photos APIs most useful. It not only documents the API, but also allows you to test your requests interactively - I probably never had gotten it to work without that testing capability. After entering your request, you can see your coding example in cURL, HTTP or JAVASCRIPT - but nothing for Python.

除了提供我的专辑列表之外,我也有兴趣

Besides producing a list of my albums, I was also interested in

  • 链接到每个相册,
  • 图像列表(是否在相册中)
  • 链接到我的每个媒体项目以及找到它们的URL

为了获得专辑的链接,您可以简单地通过检索item['productUrl']来扩展上述示例.但是,很多时候该URL在Firefox,IE或Edge中都不适用于我(非常简短地显示相册后出现错误404),但在Chrome和Opera中却不起作用(他们知道为什么).

In order to get the link to the albums, you can extend the above examples simply by retrieving item['productUrl']. However, many times the URL did not work for me in Firefox, IE nor Edge (error 404 after showing the album very briefly), but it did in Chrome and Opera (who knows why).

更可靠的似乎是专辑封面照片的网址:item['coverPhotoMediaItemId'],您可以在 Info 下找到指向该专辑的链接.

Much more reliable seems to be the URL of the album's cover photo: item['coverPhotoMediaItemId'] and there you'll find links to the album(s) under Info.

除了使用albums方法之外,您还可以访问sharedAlbums(并指定results.get('sharedAlbums', []).我希望能够获得shareableUrl,但从未找到ShareInfo资源作为其中的一部分).结果.

Instead of using the albums method, you can also access sharedAlbums (and specify results.get('sharedAlbums', []). I was hoping to be able to get the shareableUrl, but never found the ShareInfo resource as part of the results.

对于图像列表,可以选择两种方法:mediaItems.listmediaItems.search.我认为前者没有用,因为它会返回一长串的 所有 您的图像,而搜索允许按日期限制结果,则拍摄了照片(尚未上传!).还有一个getbatchGet,我从未尝试过,因为您需要知道Google照片赋予该图像的项目ID.

For the list of images, you have the choice of two methods: mediaItems.list and mediaItems.search. I don't consider the former useful since it returns a long list of all your images, while the search allows for limiting the results by the date, the picture was taken (not uploaded!). There is also a get and batchGet, I never tried because you need to know item ID given to the image by Google photos.

每个方法对于要返回的最大条目数都有一个限制(pageSize).如果有更多内容,它还会发送pageToken,您可以用来请求下一部分.

Each method has a limit (pageSize) for the maximum of entries to be returned. If there are more than that, it also sends a pageToken, you can use to request the next part.

我终于想出了这个例子:

I finally came up with this example:

from os.path import join, dirname
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'

store = file.Storage(join(dirname(__file__), 'token-for-google.json'))
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets(join(dirname(__file__), 'client_id.json', SCOPES))
    creds = tools.run_flow(flow, store)
google_photos = build('photoslibrary', 'v1', http=creds.authorize(Http()))

day, month, year = ('0', '6', '2019')  # Day or month may be 0 => full month resp. year
date_filter = [{"day": day, "month": month, "year": year}]  # No leading zeroes for day an month!
nextpagetoken = 'Dummy'
while nextpagetoken != '':
    nextpagetoken = '' if nextpagetoken == 'Dummy' else nextpagetoken
    results = google_photos.mediaItems().search(
            body={"filters":  {"dateFilter": {"dates": [{"day": day, "month": month, "year": year}]}},
                  "pageSize": 10, "pageToken": nextpagetoken}).execute()
    # The default number of media items to return at a time is 25. The maximum pageSize is 100.
    items = results.get('mediaItems', [])
    nextpagetoken = results.get('nextPageToken', '')
    for item in items:
            print(f"{item['filename']} {item['mimeType']} '{item.get('description', '- -')}'"
                      f" {item['mediaMetadata']['creationTime']}\nURL: {item['productUrl']}")

这篇关于使用google-api-python-client使用Python访问Google Photo API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆