Skip to content

Instantly share code, notes, and snippets.

@bkamapantula
Last active May 26, 2020 14:43
Show Gist options
  • Save bkamapantula/5526568152855f7b44ecd4fd29551451 to your computer and use it in GitHub Desktop.
Save bkamapantula/5526568152855f7b44ecd4fd29551451 to your computer and use it in GitHub Desktop.
Create GitHub Gist with Python requests
# this example is adapted from https://stackoverflow.com/a/50704456
import requests, json
# handler below is a Gramex FunctionHandler object
# ref: https://learn.gramener.com/guide/functionhandler/
# three arguments are passed from the user input: ?js=, ?json=, and ?md=
data = json.dumps({
"public":"true",
"files": {
"index.js": {
"content": handler.get_argument('js')
},
"data.json": {
"content": handler.get_argument('json')
},
"README.md": {
"content": handler.get_argument('md')
}
}
})
# GITHUB_ACCESS_TOKEN is a personal access token created under my profile -> developer settings
response = requests.post('https://api.github.com/gists', data=data, auth=('bkamapantula', GITHUB_ACCESS_TOKEN))
return json.dumps(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment