Home > Mobile >  Google Apps Script : API Error message "client_id is required"
Google Apps Script : API Error message "client_id is required"

Time:09-17

I've created a variable to hold the client ID (CLIENT_ID) but I keep getting an error message saying that the client ID is required when running this function. Anyone have any idea of what I've done wrong here?

function getAuth() {
  var authBasedUrl = 'https://test-api.service.hmrc.gov.uk/oauth/authorize';
  var response = UrlFetchApp.fetch(authBasedUrl,  {
      headers: {
        'Accept' : 'application/vnd.hmrc.1.0 json',
        'response_type': 'code',
        'client_id' : CLIENT_ID,
        'scope' : 'hello',
        'redirect_uri' : 'https://www.xxxxxxx.com/'
      }});
  var result = JSON.parse(response.getContentText());
  Logger.log(JSON.stringify(result, null, 2));
  } 

CodePudding user response:

Based on the docs you need to make a POST request. There is a blockqoute on the page that says:

Include the request parameters in the request body, not as request headers.

EDIT:

function getAuth() {
  var authBasedUrl = 'https://test-api.service.hmrc.gov.uk/oauth/token';
  var options = {
    headers: {
      'Accept': 'application/vnd.hmrc.1.0 json',
      "Content-Type": "application/json"
    },
    payload: JSON.stringify({
      client_id: 'RgwU6hvdxxxxxxic6LwIt',
      client_secret: '9e8c9yyyyyyyyyyc2fc2ed9126',
      grant_type: 'client_credentials',
      scope: 'hello'
    })
  }
  var response = UrlFetchApp.fetch(authBasedUrl, options);
  var result = JSON.parse(response.getContentText());

  console.log(result);
}
  • Related