How to get github token using username and password
Asked Answered
I

6

11

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.

Interlope answered 16/6, 2011 at 11:26 Comment(0)
S
5

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

Schoolhouse answered 17/6, 2011 at 0:30 Comment(2)
I have a feeling, they have changed the auth pages, they no longer seem to list the use of Autherization: basic Base64(username:password) headerUndergo
+1, the whole point of OAuth is to not have to ask for a username and password in the first place. The user shouldn't trust your app with their username and password, but they can trust github with it. That's the principle of OAuth: you open a github connection page for them, they connect and github gives your app the token.Clemmy
K
7

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))
Kinase answered 18/6, 2011 at 0:4 Comment(0)
S
5

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

Schoolhouse answered 17/6, 2011 at 0:30 Comment(2)
I have a feeling, they have changed the auth pages, they no longer seem to list the use of Autherization: basic Base64(username:password) headerUndergo
+1, the whole point of OAuth is to not have to ask for a username and password in the first place. The user shouldn't trust your app with their username and password, but they can trust github with it. That's the principle of OAuth: you open a github connection page for them, they connect and github gives your app the token.Clemmy
S
5

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/.

Sisterly answered 28/1, 2014 at 22:56 Comment(0)
S
4

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

Settlings answered 27/9, 2016 at 8:23 Comment(0)
K
0

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.

Kellum answered 16/6, 2011 at 17:49 Comment(0)
P
0

Here is the code to use GitHub Basic Authentication in JavaScript

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) {
                  }));
Phane answered 10/8, 2020 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.