Github API Basic Authentication Example

Posted by yaohong on Wednesday, July 7, 2021

TOC

Github API Basic Authentication Example

1.Generate Personal access tokens

Open this page Generate Personal access tokens and click Generate new token to get a token;

2.Use access token to request Github REST api

2.1 Install requests with pip;

pip install requests

2.2 Sustitute GITHUB_API_USER_NAME with your user name and GITHUB_API_PERSONAL_TOKEN with the token you got in step one, then run the following code;

from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects

def getRateLimit():
    url = 'https://api.github.com/rate_limit'
    print(url)
    parameters = {
    }
    headers = {
        'Accept':'application/vnd.github.v3+json',
    }
    session = Session()
    session.auth = ("GITHUB_API_USER_NAME", "GITHUB_API_PERSONAL_TOKEN")
    session.headers.update(headers)
    data = None;
    try:
        response = session.get(url, params=parameters)
        return response;
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print(e)
    return data;

if __name__ == '__main__':
    rs = getRateLimit()
    # print(rs.headers)
    print(rs.text)


## Output:
## if your access token is correct, the limit of core of resources should be 5000 rather than 60.
{"resources":{"core":{"limit":5000,"used":1245,"remaining":3755,"reset":1625664206},"search":{"limit":30,"used":0,"remaining":30,"reset":1625662728},"graphql":{"limit":5000,"used":0,"remaining":5000,"reset":1625666268},"integration_manifest":{"limit":5000,"used":0,"remaining":5000,"reset":1625666268},"source_import":{"limit":100,"used":0,"remaining":100,"reset":1625662728},"code_scanning_upload":{"limit":500,"used":0,"remaining":500,"reset":1625666268}},"rate":{"limit":5000,"used":1245,"remaining":3755,"reset":1625664206}}

REFERENCE: 1.Other authentication methods

2.RFC2617: HTTP Authentication: Basic and Digest Access Authentication

3.Github API rate limit

「点个赞」

Yaohong

点个赞

使用微信扫描二维码完成支付