In [1]:
import os
gh_user = os.environ['GITHUB_USER']
gh_token = os.environ['GITHUB_TOKEN']
Create a requests.Session
for holding our oauth token
In [2]:
import requests
s = requests.session()
s.headers['Authorization'] = 'token ' + gh_token
Verify that we have the scopes we expect:
In [3]:
r = s.get('https://api.github.com/user')
r.raise_for_status()
r.headers['X-OAuth-Scopes']
Out[3]:
Now we can make a gist!
In [4]:
import json
r = s.post('https://api.github.com/gists',
data=json.dumps({
'files': {
'test.md': {
'content': '# JupyterHub gist\n\nThis file was created from JupyterHub.',
},
},
'description': 'test uploading a gist from JupyterHub',
}),
)
r.raise_for_status()
print("Created gist: %s" % r.json()['html_url'])