Page MenuHomeSoftware Heritage
Paste P584

github token creation routine
ActivePublic

Authored by ardumont on Jan 16 2020, 1:32 PM.
#!/usr/bin/env bash
set -xe
USER=$1
PASSWORD=$2
AUTH="${USER}:${PASSWORD}"
curl -i -u $AUTH https://api.github.com
curl -i -u $AUTH \
-d '{"scopes":["public_repo"], "note": "Allow listing github repositories"}' \
https://api.github.com/authorizations
# note:
# - passwords retrieved from swh's credential store
# - token ensure working afterwards using
# curl --head -u <login>:<token> https://api.github.com/repositories
# HTTP/1.1 200 OK
# Date: Thu, 16 Jan 2020 12:26:32 GMT
# Content-Type: application/json; charset=utf-8
# Content-Length: 461244
# Server: GitHub.com
# Status: 200 OK
# X-RateLimit-Limit: 5000 # against 60 without authentication
# X-RateLimit-Remaining: 4997
# X-RateLimit-Reset: 1579180727

Event Timeline

ardumont mentioned this in Unknown Object (Maniphest Task).

Tried this in python but for some reason (come to think of it, most probably because of session issue, it did not work...)

#!/usr/bin/env python3

# documentation:
# https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization

import click
import logging
import requests
import sys

logger = logging.getLogger(__name__)


@click.command()
@click.option('--login')
@click.option('--password')
def main(login, password):
    auth = (login, password)
    url = 'https://api.github.com'
    # authenticate query
    r0 = requests.get(url, auth=auth)
    if not r0.ok:
        logger.error('Fail to authenticate, stopping. Reason: %s', r0.content)
        sys.exit(1)
    
    url = 'https://api.github.com/authorizations/'
    r = requests.post(url, auth=auth, json={
        'scope': ['public_repo'],
        'note': 'Allow listing github repositories',
    })

    if r.ok:
        data = r.json()
        token = data['token']
        logger.info('token: %s', token)
        logger.debug('\n#### Full output: %s\n####', data)
    else:
        logger.error('Failed to create token. Reason: %s', r.content)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)
    logging.getLogger('urllib3').setLevel(logging.WARN)
    main()