kennanwho
kennanwho

Reputation: 171

convert cURL to angularjs $http

I just started learning angularjs and i'm trying to get a token from an api. The curl command I have works fine when I use it in the terminal and can get the token just fine. I am having a hard time making it work using $http though.

Here's my cURL command:

curl --data "grant_type=authorization_code&client_id=CLIENT_ID&client_secret=SECRET_KEY&redirect_uri=http://localhost:9999&code=AUTH_CODE" https://www.twitchalerts.com/api/v1.0/token

Can anyone help me convert this using $http

Upvotes: 1

Views: 3170

Answers (1)

Vanya Avchyan
Vanya Avchyan

Reputation: 880

Try this:

var data = {
    grant_type     :'authorization_code',
    client_id      :'CLIENT_ID',
    client_secret  :'SECRET_KEY',
    redirect_uri   :'http://localhost:9999&code=AUTH_CODE'
};
// Simple GET request example:
$http({
    method: 'GET',//or POST
    url: 'https://www.twitchalerts.com/api/v1.0/token',
    data:data,
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

if not then try to this

....
data:data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
....

For more information about angular $http https://docs.angularjs.org/api/ng/service/$http

Upvotes: 1

Related Questions