I am attempting to upload photos to an authenticated account with google-auth-library-nodejs, mostly following the example here. I have successfully authenticated the client with OAuth2Client and am making a post request first with the image, then to create the media item. However, I keep getting this error from the mediaItems:batchCreate POST request.
media item creation status: { code: 3,
message:
'NOT_IMAGE: There was an error while trying to create this media item.' }
{ expiry_date: 1551232184753,
scopes:
[ 'https://www.googleapis.com/auth/photoslibrary.appendonly' ],
azp:
'279811607028-ne3ln378urka8e6gn1nknag92uv7qhvs.apps.googleusercontent.com',
aud:
'279811607028-ne3ln378urka8e6gn1nknag92uv7qhvs.apps.googleusercontent.com',
exp: '1551232184',
access_type: 'offline' }
I suspect this is because the Gaxios object parameter in Oauth2Client.request encodes the data of the request in base64, while the photos api docs ask for a binary data. Is there a way to override this behavior in the Gaxios object? I could make a normal post request, but I would like to keep the benefits authentication benefits of google-auth-library. Here is my code.
const oAuth2Client = await getAuthenticatedClient();
let image = fs.readFileSync(path.join(__dirname, '../diagrams/camera_mod_measurements.jpg') );
// Make a simple request to the People API using our pre-authenticated client. The `request()` method
// takes an GaxiosOptions object. Visit https://github.com/JustinBeckwith/gaxios.
const requestParams = {
url: "https://photoslibrary.googleapis.com/v1/uploads",
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"X-Goog-Upload-File-Name": "test1",
"X-Goog-Upload-Protocol": "raw"
},
data: {
MEDIA_BINARY_DATA: image
},
params: {
},
}
const uploadToken = await oAuth2Client.request(requestParams);
console.log("UPLOAD TOKEN:", uploadToken.data);
const mediaItemRequest = {
url: "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate",
method: "POST",
headers: {
"Content-Type": "application/json"
},
data: {
"newMediaItems": [
{
"description": "TEST",
"simpleMediaItem": {
"uploadToken": uploadToken.data
}
}
]
}
};
const mediaItemResponse = await oAuth2Client.request(mediaItemRequest);
console.log(mediaItemResponse.data);
console.log("media item creation status: ", mediaItemResponse.data.newMediaItemResults[0].status);
Thank you!