How can I make a curl request in ColdFusion?

I'm trying to make a mail request in the Freshbooks API, but I still can’t get it working. In the documentation, they show an example of how to make a request using Curl.

Request for api.freshbooks.com/auth/oauth/token:

            curl -X POST -H "Api-Version: alpha" -H "Content-Type: application/json" -d '{
            "grant_type": "authorization_code",
              "client_secret": "<insert your secret>",
              "code": "<insert your authorization code>",
              "client_id": "<insert your client id>",
              "redirect_uri": "https://localhost:3000/just_an_example"
            }' "https://api.freshbooks.com/auth/oauth/token"

I use ColdFusion, and this is the method that I have, but I always get the internal server 500 error.

function getAccessToken( String code ){
    var httpService = new http();
    var body = {};
    httpService.setMethod("post");
    httpService.setUrl( getApiUrl() );
    httpService.addParam(type="header", name="Content-Type", value="application/json");
    httpService.addParam(type="header",name="Api-Version",value="alpha");

    body.grant_type = "authorization_code";
    body.client_secret = getClientSecret();
    body.code = arguments.code;
    body.client_id = getClientID();
    body.redirect_uri = "https://mywebsite.com";

    serializedStr = serializeJSON( body );
    var result = httpService.send().getPrefix();

    return result;
}

What am I doing wrong?

+4
source share

Source: https://habr.com/ru/post/1682574/


All Articles