1

我正在尝试使用 Angularjs 访问 Twitter GET statuses/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(anonymous函数) @ 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 但没有效果。

4

0 回答 0