2016-07-04 61 views
0

我试图从API获取令牌。我的cURL命令似乎在我的终端中工作正常,但似乎无法使用$ http获取它。

这里是我的卷毛:虽然使用上面的代码时

$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); 
    }); 

它给了我404:

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 

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

目前我正在访问它像这样。像这样的控制台错误:

的XMLHttpRequest无法加载https://www.twitchalerts.com/api/v1.0/token?grant_type=authorization_code&c ... ct_uri = http://localhost:9999&code=RhWKTP0GACyS6VkfSpVaOlrYTWmKyAKLSwJg2Ynq。请求的资源上没有“Access-Control-Allow-Origin”标题。因此'Origin'http://localhost:9999'不允许访问。该响应具有HTTP状态代码404.

我期待JSON响应。事情是这样的:

{access_token: 'loXk8FTOFwKfrLP3bGCnJldBxuGX03a03iQdxR8A',token_type:'Bearer',refresh_token: 'IXCGDha46Q4eHBKrijmAqUwScbsMSuBy9IopXp80'} 
+0

你是如何使'$ http'请求..显示一些代码..输出你正在得到..? –

+0

如果不显示*不工作的部分,则显示工作部分不是很有用。 – JJJ

+0

@Juhana \t 对不起。我已经更新了我的帖子 – kennanwho

回答

0

你打从本地主机..不同的域作为你的错误显示No 'Access-Control-Allow-Origin' header is present on the requested resource.所以,无论是浏览器阻止您的请求被生成或在服务器资源不可外域访问。

如果浏览器没有提出请求,请尝试绕过CORS。 或者如果资源不允许,那么您需要在服务器端的文件中将Access-Control-Allow-Origin添加为true。