Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can upload photo when using the Google Photos API

Tags:

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.

like image 445
R. Sutherland Avatar asked Aug 08 '18 12:08

R. Sutherland


People also ask

What can you do with Google Photos API?

Using the Google Photos Library API your app can read, write, and share photos and videos in Google Photos. The Library API is a RESTful API with JSON payload. The structure of the API is based on the product concepts of Google Photos: Library: media stored in the user's Google Photos account.

Can I use Google Photos without uploading photos?

An Android smartphone is set up such that you are always signed in to all Google Services on your smartphone. However& you can cancel Google Photos sync on Android by toggling Backup and Sync off in Google Photos. Step 5: Toggle Backup and Sync off to stop Google Photos from uploading.

Is it better to upload photos to Google Drive or Google Photos?

Google Photos offers free (compressed) storage This is worth doing when considering Google Photos, because photos and videos uploaded to Google Drive count against your Google storage limits. Google Photos offers free storage, as long as you're willing to compress your files.


1 Answers

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'])
like image 131
MKK Avatar answered Sep 28 '22 18:09

MKK