4

I'm trying to generate OAuth 2.0 Tokens via OneLogin's API. I read through their documentation and was able to set up Postman to test, and grab the access tokens successfully.

However, when I try to do the same request programmatically via JavaScript fetch, it doesn't work.

Code:

function getAuthToken() {
  // POST Request (To get Access Token)
  var url = 'https://api.us.onelogin.com/auth/oauth2/v2/token?grant_type=client_credentials' 

  var postOptions = {
    'method': 'POST',
    'headers': {
      'Content-Type': 'application/json; charset=utf-8',
      'Authorization' : 'client_id:xxxx, client_secret:xxxx'
    },
    'redirect': 'follow'
  };
 var authToken = UrlFetchApp.fetch(url, postOptions);
}

Error

Error message

The error message indicates that grant_type is missing, but I'm passing the grant_type argument in the body of the POST request as a string (raw) like the documentation requires, and I'm still receiving the error. I even tried to write '{"grant_type": "client_credentials"}' in-line with the 'body' parameter, but still receive the same error message.

Any ideas? Thank you in advance!

0

1 Answer 1

3

You are using a JSON encoding for the parameters. The access token request must send the form parameters using application/x-www-form-urlencoded encoding.

See the OAuth2 standard here: https://www.rfc-editor.org/rfc/rfc6749#section-4.1.3

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I just had to add ?grant_type=client_credentials' to the end of the URL and it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.