Can t Upload Image with Google Photos API using NodeJS

0 votes

I have a NodeJS application that is supposed to post photographs to Google Photos using the Google Photos API for authentication. However, when I try to submit pictures, I receive the upload token without issue, but when I try to use mediaItems:batchCreate, it returns:

"status": {
    "code": 3,
    "message": "Failed: There was an error while trying to create this media item."
  }

Here is my code to convert the image into base64 :

var base64str = fs.readFileSync('1.jpg', 'base64');

And here is the request where i get response the upload token :

request({
method: 'post',
headers: {
'content-type' : 'application/octet-stream',
'Authorization': `Bearer ${userToken}`,
'X-Goog-Upload-File-Name' :  '1.jpg',
'X-Goog-Upload-Protocol' : 'raw'
},
url: `https://photoslibrary.googleapis.com/v1/uploads`,
rejectUnauthorized: false,
body: base64str
}, function(err,response,body) {
      console.log(body);
      });

So far, I've had no problems because the (body) response contains the upload token, which I have received. When I'm ready to move on to step #2, requesting the media, Items: Here is the code for batchCreate:

let reqObject = {
    newMediaItems: [
      {
        description: "Test Image Uploading",
        simpleMediaItem: {
        uploadToken: body //Body is the upload token received from prev request
        }
      }
    ]
  };
let reqObjectString = JSON.stringify(reqObject);

  request({
    method: 'post',
    headers: {
    'content-type' : 'application/json',
    'Authorization': `Bearer ${userToken}`,
    },
    url: `https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate`,
    rejectUnauthorized: false,
    body: reqObjectString
  }, function(err,response,result) {
      console.log(result);
});

The Expected Result here it's suppose to be the successful uploading media but instead i always get this respons :

{
  "newMediaItemResults": [
    {
      "uploadToken": "CAISmQMASsyg4MwSgB2y46/QI2yAAfw/uSeHZaiwGaskaT9J3SEvOtBg4bkOb9Fd0WMhE5OML8aMJMGZWNyC3Di/woTjLGD/VJMtKBk7bg1ZyK5CA+92vk0mjUWVR2LQhhTkEs02aHRr6EkCER3rxk3AqkhC+bxTIViLerUeoKigdlqozrEJXyCLM5U+Eqjdidi0e0hEFeLqs8Yz0V8YpyWQnN+zZUp/+R4pVT9fPXUOyNlsTj5OcMLrtC3Z/W9YnKhQA0O8io8LtmMBJTf04v3YPEtoNvJTd+k5Ux7qWQYJCS60V1R+hPl76jlryos3gnVaaCxHGH/TIMmtwo2zDH+vXTyktMhr82hdMON3Mm2PCfSbJawCLuHSNND9vojO11FEs7LVXAfY1UDDzVXwv4VV8tG9F3tQVI8/4YlkR+KJUjaX6/E4ZFWx67E+swqh16HN1RXXU211eCl36mPxsX35IGPEwvhpZ89LKfk+NU1fVuHB+pNLMNQ+mZOkKs0v6VLcN+nyMpOwjAstg4QUSJdCP+virY1a2uGKwYJh",      
        "status": {
        "code": 3,
        "message": "Failed: There was an error while trying to create this media item."
      }

Can anyone help me with this?

Jun 10, 2022 in Node-js by Vaani
• 7,070 points
1,634 views

1 answer to this question.

0 votes

The Google Photos API requires binary-formatted material, not base64-formatted media.

To read the media, I used React.js and FileReader. I had no choice but to use:

const reader = new FileReader()
reader.readAsArrayBuffer()

to get the array buffer and then the image got successfully uploaded.

answered Jun 10, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
0 answers

Sync contacts using Google Contacts API version 3.0 and NodeJS' Passport

I'm using passport and would like to ...READ MORE

Jun 21, 2022 in Node-js by Vaani
• 7,070 points
530 views
0 votes
0 answers

Sync contacts using Google Contacts API version 3.0 and NodeJS' Passport

I want to sync Google contacts with ...READ MORE

Jun 22, 2022 in Node-js by Vaani
• 7,070 points
673 views
0 votes
0 answers

NodeJS HTTPS API testing with mocha and super test -"DEPTH_ZERO_SELF_SIGNED_CERT"

I need to use mocha and supertest ...READ MORE

Jun 16, 2022 in Node-js by Vaani
• 7,070 points
1,058 views
0 votes
0 answers

Upload file using NodeJS and BusBoy

I'm using NodeJS to upload a file. ...READ MORE

Jun 21, 2022 in Node-js by Vaani
• 7,070 points
1,945 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,437 views
0 votes
1 answer
0 votes
1 answer

File Download on NodeJS with use opensubtitles API

The problem is the charset output (default ...READ MORE

answered Jun 7, 2022 in Node-js by Neha
• 9,020 points
991 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP