我正在使用Rhodes开发移动应用程序,希望能够访问GitHub的私有存储库。 我只有用户名和密码。
如何获取给定用户名和密码的令牌。
我正在使用Rhodes开发移动应用程序,希望能够访问GitHub的私有存储库。 我只有用户名和密码。
如何获取给定用户名和密码的令牌。
如果您只有登录名和密码,您可以使用基本身份验证(basic auth)进行身份验证。首先,请检查此代码是否向您显示所需存储库的json数据。用户名和密码必须用冒号分隔。
curl -u "user:pwd" https://api.github.com/repos/user/repo
import urllib2
import json
from StringIO import StringIO
import base64
username = "user@example.com"
password = "naked_password"
req = urllib2.Request("https://api.github.com/repos/user/repo")
req.add_header("Authorization", "Basic " + base64.urlsafe_b64encode("%s:%s" % (username, password)))
req.add_header("Content-Type", "application/json")
req.add_header("Accept", "application/json")
res = urllib2.urlopen(req)
data = res.read()
repository = json.load(StringIO(data))
Github用户可以在应用设置中创建个人访问令牌。您可以使用此令牌作为基本http身份验证的用户名/密码的替代方式,以调用API或访问Github网站上的私有存储库。
只需使用支持基本http身份验证的客户端即可。将用户名设置为该令牌,并将密码设置为x-oauth-basic
。例如,使用curl:
curl -u <token>:x-oauth-basic https://api.github.com/user
向/authorizations
发送POST请求
带有以下头部信息:
Content-Type: application/json
Accept: application/json
Authorization: Basic base64encode(<username>:<password>)
但是请记住考虑双因素身份验证https://developer.github.com/v3/auth/#working-with-two-factor-authentication
您将收到一个可用于进一步请求的令牌。
请按照 help.github.com 上的 this 指南操作。它描述了如何找到您的 API 令牌(在“帐户设置”>“帐户管理员”下),并配置 git 以使用该令牌。
这里是使用JavaScript进行GitHub基本身份验证的代码
let username = "*******";
let password = "******";
let auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var options = {
host: 'api.github.com',
path: '/search/repositories?q=google%20maps%20api',
method: 'GET',
headers: {
'user-agent': 'node.js',
"Authorization": auth
}
};
var request = https.request(options, function (res) {
}));