AngularJS动态$ http授权头

时间:2015-07-21 06:08:52

标签: angularjs

我希望我的应用程序向我的后端发送请求,我需要的是基本身份验证。我有以下代码:

myApp.factory('ApiRequest', ['$http', 'AccountService', function($http, AccountService) {

var apiURL = 'https://web.appspot.com/_ah/api/server';
var apiVer = 'v1';
var url = apiURL + '/' + apiVer;

return {
    getMessages: function(time_offset) {
        var encodedCredentials = AccountService.generateBase64Credentials();

        // $http.defaults.headers.common['Authorization'] = encodedCredentials; tried like this, but doesn't work too
        return $http.get(
            url + '/message', {
                headers: {
                    'Authorization': AccountService.generateBase64Credentials()
                },
                params: {
                    recipient_id: '6305746161500160',
                    recipient_type: 'circle',
                    time_offset: time_offset ? time_offset : null
                }
            }
        )
    }
}
}]);

不幸的是每次我获得此授权标题:

Basic W29iamVjdCBPYmplY3RdOltvYmplY3QgT2JqZWN0XQ==

我的函数AccountService.generateBase64Credentials返回字符串:基本window.btoa(登录名:密码),为什么它以这种方式工作?如何使其正常工作

1 个答案:

答案 0 :(得分:0)

您对Base64编码的输入不正确。当我解码了字符串时,它返回“[object Object]:[object Object]”(不包括双引号)。请先检查“window.btoa”功能的输入。