设置http授权标头时,AngularJS应用程序崩溃

时间:2014-06-04 14:09:06

标签: angularjs angularjs-resource angular-http angularjs-authentication

我想向twitter api发出请求并获取当前登录的userfeed。

我用“apigee”尝试了twitter api,一切正常。即使将授权标题从“apigee”复制到“邮递员”中,我也会收到数据。

好吧但是当我尝试在我的angular-app中发出http请求时,整个应用程序停止工作。 在浏览器中只有输出“{{message}}”,控制台会输出两条错误消息:

Uncaught SyntaxError: Unexpected identifier :3000/javascripts/app.js:211
Uncaught Error: No module: app angular.min.js:17

我的应用中的代码如下所示

app.controller('TwitterCtrl', function($scope, $http) {
var url = "https://api.twitter.com/1.1/statuses/home_timeline.json";
$http.get(url, 
{headers: {
  'Authorization':
  Oauth oauth_consumer_key = "xxxx",
  oauth_signature_method="HMAC-SHA1"
  oauth_timestamp="1401883718",
  oauth_nonce="840988792",
  oauth_version="1.0",
  oauth_token="xxxx",
  oauth_signature="xxx"
}})
.then(function (data) {
$scope.data = data.data;
console.log(data);
});

任何人都可以帮助我或告诉我我做错了什么。 凯文

1 个答案:

答案 0 :(得分:1)

尝试这种方式:

app.controller('TwitterCtrl', function($scope, $http) {
var url = "https://api.twitter.com/1.1/statuses/home_timeline.json";
$http.get(url, {
    headers: {
        'Authorization':
            'Oauth oauth_consumer_key = "xxxx",' +
            'oauth_signature_method="HMAC-SHA1",' +
            'oauth_timestamp="1401883718",' +
            'oauth_nonce="840988792",' +
            'oauth_version="1.0",' +
            'oauth_token="xxxx",' +
            'oauth_signature="xxx"'
    }
}).then(function (data) {
    $scope.data = data.data;
    console.log(data);
});