0

Hello i am trying to deploy an appscript project using the rest api for appscript, it gives me a 404 error in response, my code looks like this

The google appscript api is enabled on my account and i am using a gcp standard project in my script.

function createScriptDeployment(){
  var token = ScriptApp.getOAuthToken();
  var endPoint = 'https://script.googleapis.com/v1/projects';
  var headers = {
    'accept': "application/json",
    'Authorization': 'Bearer ' + token
  }
  var body = {
  'versionNumber': 2,
  'manifestFileName': 'appsscript',
  'description': 'second deployment test'
  }
  var options = {
    'method': 'POST',
    'headers': headers,
    'contentType': 'application/json',
    'payload': JSON.stringify(body),
    'muteHttpExceptions': true
  }

  var url = endPoint+"/{scriptId}/deployments";
  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response);
}

what could be the possible problem ?

any help is deeply appreciated

sudip-modi
  • 86
  • 1
  • 6
  • Please add all the relevant details directly in the question body you are using GCP standard project, have enabled the GAS API on your account and the GCP, etc. – Rubén May 25 '22 at 18:38

1 Answers1

2

In line

  var url = endPoint+"/{scriptId}/deployments";

replace {scriptId} with your script id from url

You can also use

var url = endPoint+'/' + ScriptApp.getScriptId() + '/deployments`;

If you deploy the same script you're running code at.

Edit:

Full script:

function createScriptDeployment(){
  var token = ScriptApp.getOAuthToken();
  var endPoint = 'https://script.googleapis.com/v1/projects';
  var headers = {
    'accept': "application/json",
    'Authorization': 'Bearer ' + token
  }
  var body = {
  'versionNumber': 2,
  'manifestFileName': 'appsscript',
  'description': 'second deployment test'
  }
  var options = {
    'method': 'POST',
    'headers': headers,
    'contentType': 'application/json',
    'payload': JSON.stringify(body),
    'muteHttpExceptions': true
  }

  var url = endPoint+"/"+ScriptApp.getScriptId()+"/deployments";
  var response = UrlFetchApp.fetch(url,options);
  Logger.log(response);
}

This code works perfectly for me.

roma
  • 1,512
  • 10
  • 20
  • thank you for the prompt response, although this gives me back the same error, any ideas? – sudip-modi May 25 '22 at 14:25
  • Your script works perfectly for me after providing script id and enabling User Script api and project script api. Did you enabled project script api? Cause it requires switching from default apps script GCP project into standart and enabling it there (at least I was unable to enable it on default GCP project) – roma May 25 '22 at 15:02
  • i have enabled the appscript api on my gcp project, how would i enable the project script api and change my project to a standard project? – sudip-modi May 25 '22 at 15:16
  • https://script.google.com/home/usersettings – roma May 25 '22 at 15:19
  • its turned on in my account, and i have added a gcp project number to my appscript project settings, same error code happens – sudip-modi May 25 '22 at 15:49
  • could you please share your code in a fiddle? – sudip-modi May 26 '22 at 11:36
  • https://jsfiddle.net/9Lv368c5/ – roma May 26 '22 at 14:54