简体   繁体   中英

Twitter Digits - retrieve user account data with AngularJS

I'm using AngularJS to retrieve the user account data from Twitter Digits.

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

When performing get request the error is:

"XMLHttpRequest cannot load https://api.digits.com/1.1/sdk/account.json . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:8000 ' is therefore not allowed access. The response had HTTP status code 400".

I have already tried to change the $httpProvider config to modify the headers but it didn't work.

The Digits route does not accept JSONP requests.

How can I perform this get request to the Twitter Digits API using AngularJS?

according to the documentation and the example, you must make an async call to your own server endpoint and that endpoint is supposed to do this and send back the user data.

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

says

/**
* 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.
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM