I was able get the upload token and a 200 status code on batchCreate
. However, the detailed response was this;
{
"newMediaItemResults": [
{
"uploadToken": "CAISqQMAqD4uLZF/uW6bVfUSjQTCRLEgg9ngZv9lS/VqwBw3puvDr2+FGxes9bCQ6VRS8Wd3hdm1sqmZ07CAclBbqR2S+JqRJoOS820km7RHGlZvODMcg1e+U5aFx5I+w59lz0NoyfmaUq4FQ7TqaoTqkW7vgOd2+s5z9qZtAYbqovR7+Az6xfUoZ7wZGwVnDDC0Q9Naf/Qu9qOo3yemZ/jVyE5gMpTj4jtAd+cb4DBCtWLK5cZREJUq18wiB3MqojF5Kv7UOo15dxgRTMR3lib+3s6W+BlBTc1LjZ7WSAr6PX1kA/wgxz74Cpo4neM4WRwPp5E+mSSHCGHpFzPKE8t5xpN71aOrM1d8WoT0jZApnDCAv5ggMUteli9BNSTZSPcZb7AJNUAssUKsDfb0F9NoZedDfrQZUlGH0UqYjj7X74Uk7qMtDCp2XS65LuiUNS2VCkOZydPg2Vtapvpgo/n+h2ib00+e3ci+H1+Zlo7OMH+j99TZof7p7fOdnOcQ2vF+EATdG4p6DatxFvE1op/M8hfvgVAYqV9h6aWUzxIDNKRSy1JKFcM7LO5DIA",
"status": {
"code": 3,
"message": "Failed: There was an error while trying to create this media item."
}
}
]
}
Can someone please explain to me what am I doing wrong?
Below is the code for the upload function;
uploadImages(images, token) {
const promises = Array.from(images).map(image => {
const formData = new FormData();
formData.append('media-binary-data', image);
console.log(formData);
return axios.post(`${UPLOAD_URL}`, formData, {
headers: {
'Content-Type': "application/octet-stream",
'X-Goog-Upload-File-Name': '',
'X-Goog-Upload-Protocol': "raw",
'Authorization': `Bearer ${token}`,
},
}).then((response) => {
return axios.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate',
{
"newMediaItems": [
{
"description": "item-description",
"simpleMediaItem": {
"uploadToken": response.data
}
}
]
}
,
{
headers: {
'Content-type': 'application/json',
'Authorization': `Bearer ${token}`,
},
})
})
});
return Promise.all(promises);
}
I believe your goal as follows.
images
ofconst promises = Array.from(images).map(image => {
isdocument.getElementById("###").files
from<input type="file" id="files" name="file" multiple>
.Modification points:
FormData()
is not used.https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate
isapplication/json
. So in this case, please useJSON.stringify
to the request body.When above points are reflected to your script, it becomes as follows. In this modification, as a sample situation, tha tags of HTML are added.
Modified script:
HTML side:
Javascript side:
Please set your access token to
token
.Note:
In this modified script, it supposes that your access token can be used for uploading the image files to Google Photo using Google Photo API. Please be careful this.
In this modified script, as a sample situation, tha tags of HTML are added. In my environment, I could confirm that the above modified script worked. But I think that above HTML tags might be different from your actual situation. So please modify above script for your actual situation.
References: