1

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);
    }
0

1 Answer 1

1

I believe your goal as follows.

  • You want to upload the image files to Google Photo using Google Photo API using axios with Javascript.
  • images of const promises = Array.from(images).map(image => { is document.getElementById("###").files from <input type="file" id="files" name="file" multiple>.

Modification points:

  • From your error message, I thought that the files might not have been able to be correctly uploaded.
    • In this modified script, FormData() is not used.
  • At the method of "mediaItems.batchCreate", after the multiple files were uploaded, their files can be used for one API call.
  • The content type of the request body of https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate is application/json. So in this case, please use JSON.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:

<input type="file" id="files" name="file" multiple>
<input type="button" onclick="run()" value="ok">

Javascript side:

Please set your access token to token.

function uploadImages(images, token) {
  const promises = Array.from(images).map(image => {
    return new Promise(r => {
      axios.post("https://photoslibrary.googleapis.com/v1/uploads", image, {
        headers: {
          'Content-Type': "application/octet-stream",
          'X-Goog-Upload-File-Name': image.name,
          'X-Goog-Upload-Protocol': "raw",
          'Authorization': `Bearer ${token}`,
        }
      }).then((response) => {
        r({description: "item-description", simpleMediaItem: {fileName: image.name, uploadToken: response.data}});
      });
    });
  });
  return Promise.all(promises).then(e => {
    return new Promise((resolve, reject) => {
      axios.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate',
        JSON.stringify({newMediaItems: e}),
        {headers: {'Content-type': 'application/json', 'Authorization': `Bearer ${token}`},
      })
      .then(resolve)
      .catch(reject);
    });
  });
}

// This function is run.
function run() {

  const token = "###";  // Please set your access token.

  const files = document.getElementById("files").files;
  uploadImages(files, token)
  .then(e => console.log(e))
  .catch(err => console.log(err));
}

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:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.