将照片上传到Google Photos API时未返回上传令牌

时间:2018-08-05 11:39:23

标签: java http google-photos google-photos-api

我正在使用2018年版的Google Photos API上传图像和媒体,如此处所述:“ Uploading Bytes

当我上传 new 图片或视频时,我在响应的正文中从未得到上传令牌。它始终是一个空主体,根据上面的链接,这意味着字节已经上传(但这是新上传)。

这是示例请求/响应:

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错误。

有人可以提供有关如何将字节上传到Google Photos API的有效示例吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

我认为您只是没有在查看响应内容。以下内容可在Python中运行,当然您也可以使它也适用于Java:

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()

答案 1 :(得分:1)

您实际上愿意获得回复正文。如果您看您的回应

[[(5, 4), (5, 3)], [(0, 0), (1, 0), (1, 1)], [(2, 2)]]

存在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 字段,该字段指示正文的字节长度,在您的情况下为510。您使用哪种方法获取此信息?那么您目前的实现方式是什么?

如果您仅尝试执行请求,例如通过Content-Length,然后我实际上不知道为什么没有响应主体,但是无论如何它都不适合java标记的范围,所以我假设您自己以某种自我方式解析了响应,编写Java代码?

因为您没有提供它,所以这只是推测,但是我假设您在解析响应时错过了一些东西。

答案 2 :(得分:1)

这对我来说是正常的,通过以下方法返回的r.content包含上传令牌,您需要将该令牌传递给MediaItems:batchCreate API调用,以便在用户库中创建媒体:

def upload(your_access_token, your_media_path):
item = open(your_media_path, 'rb').read()
url = 'https://photoslibrary.googleapis.com/v1/uploads'
headers = {
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/octet-stream',
    'X-Goog-Upload-File-Name': 'image1.jpg',
    'X-Goog-Upload-Protocol': 'raw',
}

r = requests.post(url, data = item, headers = headers)
return r.content.decode('utf-8')