Skip to content

Instantly share code, notes, and snippets.

@githubutilities
Last active March 30, 2018 17:20
Show Gist options
  • Select an option

  • Save githubutilities/157026e1aee66bcfd87f to your computer and use it in GitHub Desktop.

Select an option

Save githubutilities/157026e1aee66bcfd87f to your computer and use it in GitHub Desktop.
OAuth
#! /usr/local/bin/python
def create_github_repo(name, token):
import requests
import json
from urlparse import urljoin
GITHUB_API = 'https://api.github.com'
url = urljoin(GITHUB_API, 'user/repos')
payload = {}
header = {
'content-type' : 'application/json',
'Authorization': 'token ' + token
}
payload["name"] = name
#
# making request
#
res = requests.post(
url,
data = json.dumps(payload),
headers = header
)
# if status_code is 201 return True
return res.status_code == 201
if __name__ == '__main__':
name = ""
token = ""
print create_github_repo(name=name, token=token)
#! /usr/local/bin/python
# get personal access token
# by default it return ''
def get_github_personal_access_token(username, password, note, scope=["public_repo"]):
GITHUB_API = 'https://api.github.com'
import requests
import json
from urlparse import urljoin
# This API can only be accessed with username and password Basic Auth
url = urljoin(GITHUB_API, 'authorizations')
payload = {}
payload['note'] = note
payload['scopes'] = scope
header = {
'content-type': 'application/json'
}
#
# Make request
#
res = requests.post(
url,
auth = (username, password),
data = json.dumps(payload),
headers = header,
)
j = json.loads(res.text)
ret = j.get('token', '')
return ret
if __name__ == '__main__':
import getpass
username = raw_input('Github username: ')
password = getpass.getpass('Github password: ')
note = raw_input('Note about the personal access token')
print get_github_personal_access_token(password=password, username=username, note=note)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment