1

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

2
  • 1
    So where's the $http code tried. What does your error handler tell you? What do you see in browser console when try to make request? Commented Jul 4, 2016 at 21:11
  • Just the fact that it has client secret keys in it suggests you probably can't do this with ajax anyway since it would mean exposing your credentials in browser Commented Jul 4, 2016 at 21:13

1 Answer 1

1

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

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

3 Comments

data and params are not interchangeable for GET and POST and GET would have no contentType
@charlietfl when you submit the for whith method GET,enctype="application/x-www-form-urlencoded | multipart/form-data | text/plain" ,What are you think, which of these header they send? :)
Well GET has no request body so no Content Type is needed. All the data is in the url

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.