Keep getting "Unsupported grant type" error for UPS OAuth API

333 Views Asked by At

I keep getting an "Unsupported grant type: " error when trying to run the UPS OAuth API. It works in their documentation using the "Try it" feature but in my actual code it's not working and I've basically copy and pasted the code they provide in their documentation.

Here's my code:

const formData = {
    grant_type: 'client_credentials'
  };

  const url = 'https://wwwcie.ups.com/security/v1/oauth/token';
  const clientID = {clientID};
  const clientSecret = {clientSecret};
  const auth = 'Basic ' + Utilities.base64Encode(`${clientID}:${clientSecret}`);

  let options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': auth
    },
    payload: JSON.stringify(formData)
  }

Here's the error message

{ [Exception: Request failed for https://wwwcie.ups.com returned code 400. Truncated server response: {"response":{"errors":[{"code":"10400","message":"Unsupported grant type : "}]}} (use muteHttpExceptions option to examine full response)] name: 'Exception' }

Can anyone help me figure out what's going on? The documentation says that "client_credentials" is the only valid value so I'm not really sure how to proceed.

1

There are 1 best solutions below

0
Tanaike On

From your showing script, if you have wanted to use "Create Token", how about the following modification? When I saw the official document, the sample curl command was as follows.

curl -i -X POST \
  -u <username>:<password> \
  https://wwwcie.ups.com/security/v1/oauth/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'x-merchant-id: string' \
  -d grant_type=client_credentials

When this is converted to Google Apps Script, it becomes as follows.

Sample script:

Please set your values of username and password. Although I'm not sure about your actual situation, are the values of clientID and clientSecret in your showing script username and password, respectively?

function myFunction() {
  const username = "username"; // Please set your value.
  const password = "password"; // Please set your value.

  const url = "https://wwwcie.ups.com/security/v1/oauth/token";
  const auth = 'Basic ' + Utilities.base64Encode(`${username}:${password}`);
  const options = {
    headers: { "Authorization": auth, "x-merchant-id": "string" },
    payload: { grant_type: 'client_credentials' }
  }
  const res = UrlFetchApp.fetch(url, options);
  console.log(res.getContentText());
}

Note:

  • I think that the request of this Google Apps Script is the same as the above curl command. But, if an error occurs, please confirm your values again.

References: