python - 使用 For 循环将照片上传到 Google 相册
问题描述
我正在使用 Google Photos API 将超过 200 万张图片上传到我的 Google 帐户。目标是编写一个 for 循环来遍历目录中的所有图像并将它们上传到谷歌照片。for 循环在一段时间内可以正常工作,但最终会停止工作。这是代码:
i=0
k=0
tokens = []
upload_url = 'https://photoslibrary.googleapis.com/v1/uploads'
service = Service(API_SERVICE_NAME, VERSION, SCOPES, SECRET_ID=cred)
for file in files:
if file.endswith(".JPG" ) or file.endswith(".jpg" ):
i = i + 1
token = pickle.load(open('token_photoslibrary_v1.pickle', 'rb'))
image_path = os.path.join(image_dir, file)
# response = upload_images(image_path, file, token)
headers = {
'Authorization': 'Bearer ' + token.token,
'Content-type': 'application/octet-stream',
'X-Goog-Upload-Protocol': 'raw',
'X-Goog-File-Name': file,
'X-Goog-Upload-File-Name': view+file
}
img = open(image_path, 'rb').read()
response = requests.post(upload_url, data=img, headers=headers)
tokens.append(response.content.decode('utf-8'))
if i%50==0 and i>0: #call every 50
k = k + 1
print(f'{k} batch is uploading')
new_media_items = [{'simpleMediaItem': {'uploadToken': tok}} for tok in tokens]
request_body = {
'newMediaItems': new_media_items
}
upload_response = service.mediaItems().batchCreate(body=request_body).execute()
tokens = []
错误是:
<HttpError 400 when requesting https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate?alt=json returned "Request must contain a valid upload token.". Details: "Request must contain a valid upload token.">
我的帐户已激活 Google One,我有 2T 空间。
解决方案
推荐阅读
- reactjs - useEffect 中的 useState 不更新状态
- excel - 查找两个相同大小的行之间的最大差异,同时忽略 Excel 中的空白单元格
- jms - ActiveMQ Artemis 升级问题:HornetQ to Artemis 2.0
- python - 为什么默认日期时间在 python 中没有改变?
- spring - Java Springboot MQTT IoT AWS 连接丢失,EOF 异常
- java - Android中的自定义布局而不是键盘
- arrays - Jq - 计算 JSON 中每个数组的长度并更新它
- javascript - Javascript - 相等函数的行为方式不同
- sql - 不通过修改sql查询在网页上显示完整的URL
- javascript - 材质 UI 无法导入和使用 ClickAwayListener