如何使用angularjs进行基本身份验证?

时间:2015-06-27 21:15:37

标签: angularjs authentication github-api

在github中,这个部分你可以做基本的身份验证...如果我使用curl,这看起来像:

$ curl -u <username>:<token> https://api.github.com/user

如果我以角度方式进行,请如何将其转换为http请求?

1 个答案:

答案 0 :(得分:1)

我终于设法解决了这个问题。我的答案来自icodeya

function authenticateUser(username, authtoken) {

  var url = "https://api.github.com/user"

  var credentials = btoa(username + ':' + authtoken);
  var authorization = {'Authorization': 'Basic ' + credentials};
  var header = { headers: authorization }

  return $http.get(url, header)
     .then( function(response) {
        function() { //do something here. }
     }
  );
}