繁体   English   中英

AngularJS动态$ http授权标头

[英]AngularJS dynamic $http authorization header

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

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返回字符串:Basic window.btoa(login:password),为什么它如此工作? 我如何使其正常工作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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