Twitter数字 - 使用AngularJS检索用户帐户数据

时间:2015-09-23 10:37:14

标签: javascript angularjs twitter twitter-digits

我使用AngularJS从Twitter数字中检索用户帐户数据。

Digits.logIn()
            .done(onLogin)
            .fail(onLoginFailure);



function onLogin(loginResponse) {
       console.log('Digits login succeeded.');
       var oAuthHeaders = parseOAuthHeaders(loginResponse.oauth_echo_headers);
       apiURL = oAuthHeaders.apiUrl;
       credentials = oAuthHeaders.credentials;

   $http.get(apiURL,{
           headers: {'Authorization': credentials}
       }).then(function(response) {
           // this callback will be called asynchronously
           // when the response is available
           console.log(response);
       }, function(response) {
           // called asynchronously if an error occurs
           // or server returns response with an error status.
           console.log(response);
       });
}

执行get请求时,错误为:

" XMLHttpRequest无法加载https://api.digits.com/1.1/sdk/account.json。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://localhost:8000'因此不允许访问。响应的HTTP状态代码为400"。

我已经尝试更改$ httpProvider配置来修改标题,但它没有用。

Digits路由不接受JSONP请求。

如何使用AngularJS对Twitter Digits API执行此获取请求?

1 个答案:

答案 0 :(得分:1)

根据文档和示例,您必须对您自己的服务器端点进行异步调用,并且该端点应该执行此操作并发回用户数据。

https://github.com/twitterdev/cannonball-web/blob/master/public/javascripts/cannonball.js

/**
* Handle the login once the user has completed the sign in with Digits.
* We must POST these headers to the server to safely invoke the Digits API
* and get the logged-in user's data.
*/
相关问题