简体   繁体   中英

Got code 422 error when convert cURL to google app script

Here is what the document says:

Use the code parameter value to make the following request to the OAuth token endpoint in the API with the authorization_code grant type:

curl --location --request POST 'https://api.deliverr.com/oauth/v1/token' \\
--header 'Content-Type: application/x-www-form-urlencoded' \\
--data-urlencode 'code={received_code_value}' \\
--data-urlencode 'grant_type=authorization_code'

And I tried to use google app script make the code like below

function testGetToken(){
  var url = "https://api.staging.deliverr.com/oauth/v1/token"
  /*const payload = {
      'code':'this is code',
      'grant-type':'authorization_code'
  };*/
  var headers = {
            "code": "this is code",
            "grant_type": "authorization_code",
            "Content-Type": "application/x-www-form-urlencoded"
        };
  const options = {
      'method': 'POST',
      'header': headers
      //'payload': payload
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());

}

No matter I put code and grant_type to payload or header They all return same message

Exception: Request failed for https://api.staging.deliverr.com returned code 422. 
Truncated server response: 
{"code":422,"message":"{"fields":{"request.grant_type":
{"message":"'grant_type' is required"}}}\n
Please refer to Deliverr API documentation... 
(use muteHttpExceptions option to examine full response)

What is going on for my code? Is that urlencode problem or something else? How to make it work?

I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script.

     curl --location --request POST 'https://api.deliverr.com/oauth/v1/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'code={received_code_value}' \ --data-urlencode 'grant_type=authorization_code'
  • You have already confirmed that your curl command worked fine.

In this case, how about the following modification?

Modified script:

function testGetToken() {
  const url = "https://api.deliverr.com/oauth/v1/token";
  const payload = {
    'code': '{received_code_value}',
    'grant-type': 'authorization_code'
  };
  const options = { payload };
  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}
  • When payload is used with UrlFetchApp, the POST method is automatically used.
  • The default content type of the request header is application/x-www-form-urlencoded .
  • In your curl command, the data is sent as the form.

Note:

  • I think that the request of the above sample script is the same as your curl command. But, if an error occurs, please confirm your '{received_code_value}' and 'authorization_code' again.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM