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?