我正在使用以下函数来获取特定存储库上的协作者:
var getCol = function(username, reponame) {
var repo;
var repoUrl = "https://api.github.com/repos/" + username + "/" + reponame + "/collaborators";
return $http.get(repoUrl)
.then(function(response) {
return repo = response.data;
return $http.get(repoUrl + "/collaborators");
});
};
我需要进行身份验证才能访问此类数据。假设我通过在 https://github.com/settings/applications 注册一个新应用程序来获得我的客户端 token 。
如何在 Angular 中进行身份验证,以便可以充分使用该 API?
最佳答案
当不使用基于 token 的 oauth 登录时,您应该设置一个 header ,其中包含登录 github 所需的数据。一旦设置了 header ,AngularJS 将在每个请求中发送它们。没试过,但应该是这样的:
$http.defaults.headers.common["User"] = user;
$http.defaults.headers.common["Password"] = password;
回答您的评论:
Here you can get a token for your account
并像这样使用它:
$http.defaults.headers.common["Authorization"] = 'Basic' + yourApiToken;
这根本不安全,因为您将使用您的 token 进行身份验证,如果您让其他用户执行此请求,则不应执行此操作。了解更多 here为了正确执行此操作,您需要让用户使用其通行证和用户名登录,并发送此数据以获取用户 session 的 token 。
关于javascript - 验证我的 Angular 应用程序以便在 github 存储库上获得协作者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829508/