Home > Software engineering >  Google Photos API: 400 error when making request to update mediaItem description
Google Photos API: 400 error when making request to update mediaItem description

Time:06-08

I am trying to "patch" (update) a mediaItem in my Google Photos library to set a new description and am getting an unexpected 400 error. I am referring to the article at https://developers.google.com/photos/library/guides/manage-media#changing_media_item_descriptions for how to do it. I am writing in a Google Apps Script project and other API calls are working fine, like to list albums and list mediaItems inside albums. Permission scopes are https://www.googleapis.com/auth/photoslibrary.readonly https://www.googleapis.com/auth/photoslibrary.edit.appcreateddata.

Here is what my code looks like:

// thisMediaItem is a media item returned from a previous call
            var id = thisMediaItem.id;
            Logger.log("Here is the full JSON for the media item "   id);
            Logger.log(JSON.stringify(thisMediaItem));

            // now try "patching" the media item with a new description
            var url = 'https://photoslibrary.googleapis.com/v1/mediaItems/'   id   "?updateMask=description";
            var response = UrlFetchApp.fetch(url, {
              headers: { Authorization: 'Bearer '   photoService.getAccessToken() },
              muteHttpExceptions: true,
              method: "patch",
              payload: { "description": "my new description" }
            });
            Logger.log(response.getContentText());

And here is what I get in the logs, something about the description field not found, although I did include it in the payload. I have tried removing quotes from the member name and it still doesn't work.

5:12:24 PM  Info    Here is the full JSON for the media item AHndD0EfrVXtMUHxuCOT3V81D6ZUr8YkRz3a6PvZE6k8PCF9pMCh4gxj1IHxkVVrkpVnpQ12x5J9nznJICJ7ciDOtQbn2hJ6DA
5:12:24 PM  Info    {"id":"AHndD0EfrVXtMUHxuCOT3V81D6ZUr8YkRz3a6PvZE6k8PCF9pMCh4gxj1IHxkVVrkpVnpQ12x5J9nznJICJ7ciDOtQbn2hJ6DA","description":"lake","productUrl":"https://photos.google.com/lr/album/AHndD0FrK2VQxeFhlCuOrJFV-C8YrwjI_eM2nFnBTuVh_S1QerqpNOFoS8o2X0tz7Xh_DJLnH68s/photo/AHndD0EfrVXtMUHxuCOT3V81D6ZUr8YkRz3a6PvZE6k8PCF9pMCh4gxj1IHxkVVrkpVnpQ12x5J9nznJICJ7ciDOtQbn2hJ6DA","baseUrl":"https://lh3.googleusercontent.com/lr/AFBm1_bpo-9VZkKEUKVmLGwZifhWZe_18r5R1eWh53o5YghfnyFH11JOFSU3L_6gc6h5xktjE7qHKAYtBX5yEQoEOhkGfxFQjmKcHXUa52QBUjg2jMvyyIpoVjP27fYP5GPUEUn7ncADwqLeTSKBbIF-sGZuKt2QBaYNR3a3icFxBUz8x2mC_PIU6SsKWeVrKTO8v6yx84GjfSN3XbDlmRKLdAiZ7OpBXk6TP4pLEVONrkEKGpX_cy_-1AEejEF3hQNgqxwlPmtzrbiRJIO25gfCLANEdxMVt_LjmM5Avb0TiZTZ2SdUrUMWQNA0YAGH6D7KE79CTF34jKfui_EM5_eslwujqRwwoh_l6oorsCqMyVHjHqqfQ2QIPEa3VepucQD5RiIWXEImXSQaxkChz0CF-KB7u9vuj-sS8TJB2CefsHuhNiyq0owvEB8swZM07RpWKm1-4mlS286uCKgfPOeNWyWJETOWs9u1GJHCVH4N1zj2MhxSaGjAHD-3H9OgcIIK56mBJiNjLmOCJY_uTMz-tJ8INdBSp8vv979bREzXfcB23s7dvrz11sjvi7YcVRRDRchq0kEuyuXoC87QyY0YBt-QlSmcU7yXZEQ_ZwZsTzQJXawtMCffDYumUCjBCcx3mrlahGTHNGmjsWDm5SHi3eolC6rBWxno1B2S_2Z-5_vjHfbLMTyIzEmg6YuB__0vIdyaY4E5DNavqL7AF-y4T79eKru9bCXgGUDeBb52qySsAJHny14tofcWdu0bc0fwXjAFH1ZxwRbQTIXPXz1uHPea81ksZai-zGzcax6WOC-0awE0IEWCq5NB2goplxmrMLReHO_nT1L1cQ","mimeType":"image/jpeg","mediaMetadata":{"creationTime":"1966-01-01T20:00:26Z","width":"2640","height":"1778","photo":{"cameraMake":"Nikon","cameraModel":"LS-5000"}},"filename":"slide0011.jpg"}
5:12:24 PM  Info    {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"description\": Cannot bind query parameter. Field 'description' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"description\": Cannot bind query parameter. Field 'description' could not be found in request message."
          }
        ]
      }
    ]
  }
}

CodePudding user response:

In your script, please modify it as follows.

From:

var response = UrlFetchApp.fetch(url, {
  headers: { Authorization: 'Bearer '   photoService.getAccessToken() },
  muteHttpExceptions: true,
  method: "patch",
  payload: { "description": "my new description" }
});

To:

var response = UrlFetchApp.fetch(url, {
  headers: { Authorization: 'Bearer '   photoService.getAccessToken() },
  muteHttpExceptions: true,
  method: "patch",
  payload: JSON.stringify({ "description": "my new description" }),
  contentType: "application/json"
});

Note:

  • This modification supposes that the media is uploaded by the same client of your script and the media item ID is the valid ID. Please be careful about this.

Reference:

  • Related