How to get github token using username and password

蓝咒 提交于 2019-12-02 22:19:56
Tekkub

You should use oauth instead: http://developer.github.com/v3/oauth/

ibo.ezhe

Once you have only login and password you can use them using basic auth. First of all, check if this code shows you json data of desired repo. Username and password must be separated by a colon.

curl -u "user:pwd" https://api.github.com/repos/user/repo

If succeeded you should consider doing this request from code.

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 users can create Personal Access Tokens at their application settings. You can use this token as an alternative to username/password in basic http authentication to call the API or to access private repositories on the github website.

Simply use a client that supports basic http authentication. Set the username equal to the token, and the password equal to x-oauth-basic. For example with curl:

curl -u <token>:x-oauth-basic https://api.github.com/user

See also https://developer.github.com/v3/auth/.

Send A POST request to /authorizations With headers

Content-Type: application/json Accept: application/json Authorization: Basic base64encode(<username>:<password>)

But remember to take Two factor Authentication in mind https://developer.github.com/v3/auth/#working-with-two-factor-authentication

Here You will receive a token which can be used for further request

Follow this guide at help.github.com. It describes how to find your api-token (it's under "Account Settings" > "Account Admin") and configuring git so it uses the token.

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