Clientside GitHub Authentication

拥有回忆 提交于 2019-12-21 16:59:19

问题


I'm using a Javascript to do Basic Authentication with GitHub. For example, the following shell command gets a token from Github:

    curl -i -u uaername:password -k -d "{\"scopes\": [\"repo\"]}" https://api.github.com/authorizations

How do you achieve that with jQuery and AJAX?


回答1:


Including Basic Auth Data in HTTP Headers with jQuery

You can include basic auth details in the header using the Authorization field. You already understand how jQuery works. This snippet has the bits you're missing:

    let auth = btoa(username + ":" + password);

    jQuery.ajax({
        url: ...,
        headers: { Authorization: "Basic " + auth }
        ...
    });

Note: btoa and atob (pronounced B to A and A to B) are builtin functions, and convert to and from Base64. See the MDN docs for more information.




回答2:


Are you asking whether there is a way to get an oAuth token purely from the client side? If so, the answer is no.

But, you have some work arounds.

Github.js: https://github.com/michael/github

Gatekeeper is an open source server side component which can help with oAuth tokens management:

https://github.com/prose/gatekeeper

You could also use something like Firebase with simple login and in this case you don't need to manage any server side services:

https://www.firebase.com/docs/security/simple-login-github.html



来源:https://stackoverflow.com/questions/18124633/clientside-github-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!