2015-07-21 85 views
0

我希望我的應用程序向我的後端發送請求,並且我需要的是基本身份驗證。我有以下代碼:

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(賬號:密碼),爲何還以這種方式工作?我怎樣才能使它正常工作

回答

0

您輸入Base64編碼是不正確的。當我解碼字符串時,它返回「[object Object]:[object Object]」(不包括雙引號)。請先檢查輸入到「window.btoa」函數。

+0

我知道解碼的結果,但我不知道它可能是問題所在。謝謝! – SmiglowiecX