简体   繁体   English

对我的Angular应用程序进行身份验证,以便在github存储库上获得合作者

[英]Authenticate my Angular app in order to get collaborators on a github repo

I am using the following function in order to get back the collaborators on a specific repository: 我正在使用以下功能,以便找回特定存储库上的协作者:

   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");
            });
   };

I need to authenticate in order to be able to access this sort of data. 我需要进行身份验证才能访问此类数据。 Supposing I have my client token by registering a new app at https://github.com/settings/applications . 假设我通过在https://github.com/settings/applications注册一个新应用程序来拥有我的客户令牌。

How would I authenticate in Angular so I can fully use the API? 如何在Angular中进行身份验证,以便可以充分使用API​​?

You should set a header with the data required to sign in to github when not using token based oauth signin. 当不使用基于令牌的oauth登录时,您应该设置一个标头,以登录github所需的数据。 Once the headers are set, AngularJS will send them with every request. 设置标头后,AngularJS将随每个请求发送它们。 Haven't tried it but it should be something like this: 还没有尝试过,但是应该是这样的:

 $http.defaults.headers.common["User"] = user;
 $http.defaults.headers.common["Password"] = password;

To answer your comment: 回答您的评论:

Here you can get a token for your account 在这里,您可以获取帐户的令牌

And use it like this: 并像这样使用它:

$http.defaults.headers.common["Authorization"] = 'Basic' + yourApiToken;

This is not secure at all since you will be using your token to authenticate which you should not do if you are letting other users do this requests. 这一点都不安全,因为您将使用令牌来进行身份验证,如果让其他用户执行此请求,则不应该这样做。 Read some more here to do it right, you need to get the user to signin with their pass and username and send this data to get back a token for the users session. 在此处阅读更多内容以正确执行操作,您需要让用户使用其通行证和用户名登录,然后发送此数据以获取用户会话的令牌。

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

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