0

I'm trying to create a google script which automatically adds images to a google online album using the photoslibrary.googleapis.com rest api.

The first step involves uploading the files using the uploads function. I've tried authenticating the request using the script's OAuthToken

"Authorization": "Bearer " +  ScriptApp.getOAuthToken() 

But I get the following error response:

{ "code": 16, "message": "Authentication session is not defined." }

Is there a way to authenticate a google script rest request with the credentials of the user that is running it, so the photoslibrary.googleapis.com can be automatically accessed, granted that the owner of the galleries is the same as the one running the script?

        function photoAPI_upload(blob) {
      var api = "https://photoslibrary.googleapis.com/v1/uploads";

      var headers = { 
        "Authorization": "Bearer " +  ScriptApp.getOAuthToken(),  
        "contentType": "application/octet-stream",
        'X-Goog-Upload-File-Name': blob,
        'X-Goog-Upload-Protocol': "raw",
      };
      var options = {
          method: "post",
          headers: headers,
          payload: blob
      }
        var response = UrlFetchApp.fetch(api, options);
        Logger.log('upload-token' + response.getContentText());
        return response.getContentText();
    }
movees
  • 183
  • 1
  • 15

1 Answers1

0

I've found the response in this thread:

[How to use Google Photos API Method: mediaItems.search in Google apps script for a spreadsheet

Basically it comes down to this:

  1. Requires Oauth scopes. Add the below line to appsscript.json (View > Show project manifest) "oauthScopes": ["https://www.googleapis.com/auth/spreadsheets.currentonly", "https://www.googleapis.com/auth/photoslibrary", "https://www.googleapis.com/auth/photoslibrary.readonly", "https://www.googleapis.com/auth/script.external_request"]

  2. Also requires a standard GCP project with the appropriate Photo APIs enabled. https://developers.google.com/apps-script/guides/cloud-platform-projects

movees
  • 183
  • 1
  • 15