Reputation: 51
I have application built in NodeJS it is suppose to upload pictures to google photos using google photos API for the authentication part I'm doing successfully, i have problem when i try to upload pictures i receive the upload token without problem 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);
});
Up to here i don't have issue because (body) response is the upload token and i receive it. now when i want to go to step#2 to request the mediaItems:batchCreate here is the code :
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, thanks in advance
Upvotes: 5
Views: 1544
Reputation: 111
Your comment is absolutely correct. The google photos API expects media in binary format and not in base64 format.
I was using React.js and FileReader to read the media. I had to use:
const reader = new FileReader()
reader.readAsArrayBuffer()
to get the array buffer and then the image got successfully uploaded.
Upvotes: 0