将照片上传到Google Photos API时未返回上传令牌 [英] Uploading photo to Google Photos API not returning upload token
问题描述
我正在使用2018年版本的Google Photos API上传图像和媒体,如此处所述:"
I'm using the 2018 version of the Google Photos API to upload images and media as documented here: "Uploading Bytes"
当我上传 new 图片或视频时,我在响应的正文中从未得到上传令牌.它始终是一个空主体,根据上面的链接,这意味着字节已经上传(但这是新上传).
When I upload a new image or video I never get an upload token in the body of the response. It's always an empty body, which according to the above link means that the bytes have already been uploaded (but this is a new upload).
这是示例请求/响应:
request:
POST https://photoslibrary.googleapis.com/v1/uploads
request headers:
authorization: Bearer abcd1234
X-Goog-Upload-Protocol: raw
X-Goog-Upload-File-Name: 20140317T082917_001.jpg
content-type: application/octet-stream
content-length: 1292868
accept: application/json
-----------------------------------------------------
response: OK [200]
response headers:
Alt-Svc: [quic=":443"; ma=2592000; v="44,43,39,35"]
Server: [UploadServer]
X-GUploader-UploadID: [AEnB2UqT6y8KyZNCPyAaFeCj7I_ABIlwLJQMpltYzQ7D8blW4v3uKSlMT7dbNjFV0i_7ApzoR-i26ZtZ9kHkB7bI8n8ojgOnNA]
Content-Length: [510]
Date: [Sun, 05 Aug 2018 11:19:15 GMT]
Content-Type: [text/plain]
response body:
null
我也尝试使用X-GUploader-UploadID
标头中返回的值,但这会导致500
错误.
I've also tried using the value returned in the X-GUploader-UploadID
header, but that causes a 500
error.
有人可以提供有关如何将字节上传到Google Photos API的有效示例吗?
Can someone provide a working example of how to upload bytes to the Google Photos API?
谢谢!
推荐答案
我认为您只是在查看Response内容.以下内容可在Python中运行,当然您也可以使它也适用于Java:
I think you're just not looking at the Response content. The following works in Python, surely you can make it work for Java as well:
def upload_files(self, filepath, album_id):
filename = os.path.basename(filepath)
url = 'https://photoslibrary.googleapis.com/v1/uploads'
authorization = 'Bearer ' + creds.access_token
headers = {
"Authorization": authorization,
'Content-type': 'application/octet-stream',
'X-Goog-Upload-File-Name': filename,
'X-Goog-Upload-Protocol': 'raw',
}
with open(filepath, "rb") as image_file:
response = requests.post(url, headers=headers, data=image_file)
assert response.status_code == 200
token = response.text # !!!
return service.mediaItems().batchCreate(body=dict(
albumId=album_id,
newMediaItems=[
{"simpleMediaItem": {"uploadToken": token}}]
)).execute()
这篇关于将照片上传到Google Photos API时未返回上传令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!