0

I'm trying to create media item using Google Photos API. Endpoint is documented here. If I copy and paste my payload to an API explorer which can be found in documentation, I get success: Request inputed to an API Explorer and Successful response.

If I do the same in my node js program, I'm getting an error.

This is what I do:

const accessToken = "MyAccessToken";
payload = {
    "newMediaItems": [
        {
            "description": "picture",
            "simpleMediaItem": {
                "uploadToken": "CAIS+QIASsyg4OQLX2Ao5hy6I734/b01mjk3Mqpom6DQ24iv7ZfAYLiXAy0WpOXCWJBNHrmBs6FE+a9Axu5CML+Ryu4VGawyf4skxM763mzC5GcjMY4rS/r6IwOekBIoE/aMJLJpRr1gW8jdhVJM89+kioTx9d+shyYeQDbVI8ezb1lXGp6irc9hZl7QA6xd+msXzbLD5nb+wc5CA6du95tP3buh5R5N/Knn+NwByebdEPCusl+X3p7DZ6ha72kLthUqdvwFsp8dpnGbNQBq8AFPVNHXB4C543iq+dYiRFYtICCxO8xi2cpONVT54Jl6l9rGh3Vnidwj5IwkbsXkyiN96HfRb9XLh0rCBw4ydV6Y9+C+OmTAqlQwIKy50I/ykHyzggroeJSbgphiQwFR2EbHwAeSKdsdIB03ItnunHtf3F2LRIitDRGI1n4VUEYE1dYjrrjR791ao24Dp8J3Hg8IRb8E3vFTeYMWyOk4mh/zQGInfNBnRY2ruHH0JA"
            }
        }
    ]
};
   const response = await fetch('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${accessToken}`
        },
        payload: JSON.stringify(payload)
    });
    const json = await response.json();
    console.log(json);

And getting this response:

{ error:
   { code: 400,
     message: 'Request must have at least one newMediaItem.',
     status: 'INVALID_ARGUMENT' } }

What could go wrong here?

Any help is appreciated, thank you.

Community
  • 1
  • 1
Ignas Poška
  • 57
  • 1
  • 4
  • 7

1 Answers1

0

It looks like you are setting the JSON payload in the wrong parameter in your call to fetch. It should be set in the parameter body (and not in payload). The JSON itself looks okay.

This snippet should work:

const response = await fetch('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${accessToken}`
        },
        body: JSON.stringify(payload)
});

Source: node-fetch 'Post with JSON' example