javascript - 验证我的 Angular 应用程序以便在 github 存储库上获得协作者

标签 javascript angularjs github-api

我正在使用以下函数来获取特定存储库上的协作者:

   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/

相关文章:

javascript - CodeMirror 在实现更改功能时进入无限循环

angularjs - 当内容在容器内时, Angular 滚动不起作用

python - 如何使用 GitHub API v3 创建提交并推送到 repo?

javascript - Bootstrap 模态页脚未正确显示

javascript - JS datepicker capable of : time, date, datetime, day of week, week of year, month of year 等

javascript - AngularFire - 类型错误 : Cannot read property '$add' of undefined

python - 从 Github API 解码 base64 内容

ruby - 我可以通过 GH API 获取两次提交之间一个文件的差异数据吗?

javascript - jQuery .change 在 WooCommerce 中不起作用

php - 当 ajax 开始在后台运行时显示加载图像