Can upload photo when using the Google Photos API

我怕爱的太早我们不能终老 提交于 2019-12-06 06:43:53

问题


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."
      }
    }
  ]
}

Here is a snippet of my code:

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

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


回答1:


This worked for me.

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'])


来源:https://stackoverflow.com/questions/51746830/can-upload-photo-when-using-the-google-photos-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!