Twitter使用AngularJS访问GET状态/ user_timeline时返回错误

时间:2015-11-07 17:05:57

标签: angularjs oauth twitter-oauth angular-http angular-http-interceptors

我正在尝试使用Angularjs访问Twitter GET状态/ user_timeline 。我已经拥有了持票人令牌。我正在尝试使用Angularjs进行仅应用程序身份验证。

错误代码 选项https://api.twitter.com/1.1/statuses/user_timeline.json(匿名函数)@ angular.js:10722sendReq @ angular.js:10515serverRequest @ angular.js:10222processQueue @ angular.js:14745(匿名函数)@ angular.js:14761Scope。$ eval @ angular。 js:15989Scope。$ digest @ angular.js:15800Scope。$ apply @ angular.js:16097bootstrapApply @ angular.js:1660invoke @ angular.js:4478doBootstrap @ angular.js:1658bootstrap @ angular.js:1678angularInit @ angular.js: 1572(匿名函数)@ angular.js:28899trigger @ angular.js:3024eventHandler @ angular.js:3298 (index):1 XMLHttpRequest无法加载https://api.twitter.com/1.1/statuses/user_timeline.json。预检的响应具有无效的HTTP状态代码400

以下是我写的代码。

var twitter = angular.module("twitterServices",["ngResource"]);
var auth = angular.module("authInterceptor",[]);

twitter.service("tweets",function($http,$resource)
{
  var BearerToken = "some BEarer Token";
  $http.defaults.headers.common.Authorization = "Bearer " + BearerToken;
  $http.defaults.useXDomain = true;
  var userTimeURL = "https://api.twitter.com/1.1/statuses/user_timeline.json"
  return {
    getUserStat :  function()
    {
     return  $http.get(userTimeURL);
     $http.post(userTimeURL,
     {
       params : {user_id: 'siddmegadeth'}
     },
     {
       headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}
     }).then(function success(response)
     {
         log(response);
     });

    }
  }
});

auth.factory('intercept', [
  "$q", "$window", "$location", function($q, $window, $location, session) {
    var BearerToken = "some BEarer Token";
    return {
      request: function(config) {
        config.headers = config.headers || {};
        config.headers.Authorization = 'Bearer ' + BearerToken;
        return config;
      },
      response: function(response) {
        return response || $q.when(response);
      },
      responseError: function(rejection) {
        // your error handler
      }
    };
  }
]);

在我的主应用程序中

var app = angular.module("myApp",["authInterceptor","ngMessages","ngRoute","ngAnimate","ngTouch","ui.bootstrap","ngResource","twitterServices"]);


app.config(function($routeProvider,$httpProvider)
{
  $httpProvider.interceptors.push('intercept');
  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common['X-Requested-With'];

  $routeProvider.
  when("/",{
    templateUrl : "templates/landing.html",
    controller : "landingCtrl"
  })
  .when("/login",{
    templateUrl : "templates/login.html",
    controller : "loginCtrl"
  })
  .otherwise({redirectTo : "/"});
});


app.run(function($rootScope)
{
  console.warn("App Running");
});

在控制器中

app.controller("globalCtrl",function($scope,$rootScope,$resource,$http,tweets)
{
  $rootScope.slideLeft = function()
  {

  }
  $rootScope.slideRight = function()
  {

  }
  tweets.getUserStat().then(function(res)
  {
    console.log(res);
  });



});

我无法从Twitter检索数据。请帮忙。我还为Chrome添加了CORS插件,但仍然没有帮助。 我尝试过使用Xampp和nodejs但没有效果。

0 个答案:

没有答案