使用Google Photos API时可以上传照片 [英] Can upload photo when using the Google Photos API

查看:184
本文介绍了使用Google Photos API时可以上传照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的Google Photos API上传照片(jpg图片). 我可以获取uploadToken,但是当我尝试创建媒体项目时,出现以下错误:

I am trying to upload a photo (jpg image) using the new Google Photos API. I am able to get an uploadToken but when I try to create the media item I get the following error:

{
  "newMediaItemResults": [
    {
      "uploadToken": "CAIS+QIAkor2Yr4JcvYMMx..... ",
      "status": {
        "code": 3,
        "message": "NOT_IMAGE: There was an error while trying to create this media item."
      }
    }
  ]
}

这是我的代码的片段:

import sys
import json
import requests

pic = 'image.jpg'

fname = 'read_write_token_creds.json'
with open(fname) as f:
        data = json.load(f)
tok = data['access_token']

# Step 1 get an upload token

URL = 'https://photoslibrary.googleapis.com/v1/uploads'

headers = {
'Content-type': 'application/octet-stream',
'X-Goog-Upload-File-Name': pic,
'X-Goog-Upload-Protocol': 'raw',
'Authorization': 'Bearer ' + tok,
}

files = {'file': open(pic, 'rb')}
r = requests.post(URL, headers=headers, files=files)
upload_token = r.text

# Step 2

album_id = 'AG.....7u'
URL = 'https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate'

header = {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + tok
}


payload = {
  'albumId': album_id,
  'newMediaItems': [
    {
      'description': 'Desc.',
      'simpleMediaItem': { 'uploadToken': upload_token }
    }
  ]
}

r = requests.post(URL, headers=header, data=json.dumps(payload))

当我从请求模块查看r.text时,我收到了错误消息,该错误消息位于他的消息顶部.

When I look at r.text from the requests module, I receive the error message which was given at the top of he message.

推荐答案

这对我有用.

如何验证用户身份,请参见 https://developers.google.com/photos/library/guides/upload-media

How to authenticate user see here https://developers.google.com/photos/library/guides/upload-media

    def upload(service, file):
        f = open(file, 'rb').read();

        url = 'https://photoslibrary.googleapis.com/v1/uploads'
        headers = {
            'Authorization': "Bearer " + service._http.request.credentials.access_token,
            'Content-Type': 'application/octet-stream',
            'X-Goog-Upload-File-Name': file,
            'X-Goog-Upload-Protocol': "raw",
        }

        r = requests.post(url, data=f, headers=headers)
        print '\nUpload token: %s' % r.content
        return r.content

    def createItem(service, upload_token, albumId):
        url = 'https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate'

        body = {
            'newMediaItems' : [
                {
                    "description": "test upload",
                    "simpleMediaItem": {
                        "uploadToken": upload_token
                    }  
                }
            ]
        }

        if albumId is not None:
            body['albumId'] = albumId;

        bodySerialized = json.dumps(body);
        headers = {
            'Authorization': "Bearer " + service._http.request.credentials.access_token,
            'Content-Type': 'application/json',
        }

        r = requests.post(url, data=bodySerialized, headers=headers)
        print '\nCreate item response: %s' % r.content
        return r.content

# authenticate user and build service
upload_token = upload(service, './path_to_image.png')
response = createItem(service, upload_token, album['id'])

这篇关于使用Google Photos API时可以上传照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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