0

I'm trying to get a token from an API. My cURL command seems to work fine in my terminal, can't seem to get it using $http though.

Here's my cURL:

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

and

curl --data "access_token=ACCESS_TOKEN&name=Kennan&identifier=IDENTIFIER&amount=50&currency=USD&message=TESTTESTETSTS" https://www.twitchalerts.com/api/v1.0/donations

Currently I'm accessing it like this:

$http.get('https://www.twitchalerts.com/api/v1.0/token?grant_type=authorization_code&client_id=CLIENT_ID&client_secret=SECRET_KEY&redirect_uri=http://localhost:9999&code=' + AUTH_CODE)
    .success(function(data) {
        console.log(data)
    })
    .error(function(data) {
        alert(data);
        console.log('Error: ' + data);
    });

It's giving me a 404 though when using the code above. and console error like this:

XMLHttpRequest cannot load https://www.twitchalerts.com/api/v1.0/token?grant_type=authorization_code&c…ct_uri=http://localhost:9999&code=RhWKTP0GACyS6VkfSpVaOlrYTWmKyAKLSwJg2Ynq. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9999' is therefore not allowed access. The response had HTTP status code 404.

I'm expecting a JSON response. Something like this:

{access_token: 'loXk8FTOFwKfrLP3bGCnJldBxuGX03a03iQdxR8A',token_type:'Bearer',refresh_token: 'IXCGDha46Q4eHBKrijmAqUwScbsMSuBy9IopXp80'}
JJJ
  • 32,902
  • 20
  • 89
  • 102
kennanwho
  • 171
  • 1
  • 3
  • 14

1 Answers1

0

You are hitting a different domain from your localhost .. As your error shows No 'Access-Control-Allow-Origin' header is present on the requested resource. So, either the browser is blocking your request to be generated or the resource over server is not accessible outside the domain.

If browser is not making request.. Try this to bypass CORS . Or if resource is not allowing then you need to add Access-Control-Allow-Origin to true in file at server end.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65