How to get github token using username and password

Sivakumar picture Sivakumar · Jun 16, 2011 · Viewed 25.5k times · Source

I am developing mobile apps using rhodes. I want to access private repo of github. I am having only username and password.

How to get token of given username and password.

Answer

ibo.ezhe picture ibo.ezhe · Jun 18, 2011

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 = "[email protected]"
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))