gpt4 book ai didi

python - 谷歌照片 API + python : Working non-deprecated example

转载 作者:行者123 更新时间:2023-12-01 06:52:38 27 4
gpt4 key购买 nike

我一直在寻找这样的代码示例组合。但没有维护的库( google-auth )+完整的工作示例。不再支持 google-api-python-clientoauth2client ( https://github.com/googleapis/google-api-python-client/issues/651 )。

这是一个包含已弃用库的工作示例,但我希望看到一些允许完全访问 api ( searching by albumId currently doesn't work with this library ) 的示例:

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Photo v1 API
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

# Call the Photo v1 API
results = service.albums().list(
pageSize=10, fields="nextPageToken,albums(id,title)").execute()
items = results.get('albums', [])
if not items:
print('No albums found.')
else:
print('Albums:')
for item in items:
print('{0} ({1})'.format(item['title'].encode('utf8'), item['id']))

最佳答案

  • 您希望使用 google_auth 而不是 oauth2client,因为 oauth2client 已弃用。
  • 您已经能够使用 Photo API。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。

例如,授权示例脚本可以在 the Quickstart of Drive API with python 看到。可以看到安装库的方法。使用它,您的脚本可以修改如下。

修改后的脚本:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request


def main():
credentialsFile = 'credentials.json' # Please set the filename of credentials.json
pickleFile = 'token.pickle' # Please set the filename of pickle file.

SCOPES = ['https://www.googleapis.com/auth/photoslibrary']
creds = None
if os.path.exists(pickleFile):
with open(pickleFile, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentialsFile, SCOPES)
creds = flow.run_local_server()
with open(pickleFile, 'wb') as token:
pickle.dump(creds, token)

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

# Call the Photo v1 API
results = service.albums().list(
pageSize=10, fields="nextPageToken,albums(id,title)").execute()
items = results.get('albums', [])
if not items:
print('No albums found.')
else:
print('Albums:')
for item in items:
print('{0} ({1})'.format(item['title'].encode('utf8'), item['id']))


if __name__ == '__main__':
main()
  • 关于检索专辑列表的脚本,使用了您的脚本。
  • 当您运行此脚本时,首先会运行授权过程。所以请授权范围。此过程只需运行一次。但如果您想更改范围,请删除pickle文件并重新授权。

引用文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

添加 1:

如果您想使用the method of mediaItems.search ,下面的示例脚本怎么样?关于授权的脚本,请使用上面的脚本。

示例脚本:

service = build('photoslibrary', 'v1', credentials=creds)
albumId = '###' # Please set the album ID.
results = service.mediaItems().search(body={'albumId': albumId}).execute()
print(results)

添加 2:

  • 您想要从我建议的上述示例脚本中删除 googleapiclient
  • 您想要使用 google_auth_oauthlib.flowgoogle.auth.transport.requests 检索访问 token 。
  • 您想要使用 python 的 request 检索特定相册中的媒体项目列表,而不使用 googleapiclient

如果我的理解是正确的,这个示例脚本怎么样?

示例脚本:

在使用此脚本之前,请设置albumId变量。

from __future__ import print_function
import json
import pickle
import os.path
import requests
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request


def main():
credentialsFile = 'credentials.json'
pickleFile = 'token.pickle'

SCOPES = ['https://www.googleapis.com/auth/photoslibrary.readonly']
creds = None
if os.path.exists(pickleFile):
with open(pickleFile, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentialsFile, SCOPES)
creds = flow.run_local_server()
with open(pickleFile, 'wb') as token:
pickle.dump(creds, token)

albumId = '###' # <--- Please set the album ID.

url = 'https://photoslibrary.googleapis.com/v1/mediaItems:search'
payload = {'albumId': albumId}
headers = {
'content-type': 'application/json',
'Authorization': 'Bearer ' + creds.token
}
res = requests.post(url, data=json.dumps(payload), headers=headers)
print(res.text)


if __name__ == '__main__':
main()

注意:

  • 在这种情况下,您可以使用 https://www.googleapis.com/auth/photoslibrary.readonlyhttps://www.googleapis.com/的范围auth/photoslibrary.

引用:

关于python - 谷歌照片 API + python : Working non-deprecated example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58928685/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com