GitHub-Flask depends on the requests library.
Install the extension with the following command:
$ pip install GitHub-Flask
Here’s an example of how GitHub-Flask is typically initialized and configured:
from flask import Flask from flask.ext.github import GitHub app = Flask(__name__) app.config['GITHUB_CLIENT_ID'] = 'XXX' app.config['GITHUB_CLIENT_SECRET'] = 'YYY' # For GitHub Enterprise app.config['GITHUB_BASE_URL'] = 'https://HOSTNAME/api/v3/' app.config['GITHUB_AUTH_URL'] = 'https://HOSTNAME/login/oauth/' github = GitHub(app)
The following configuration settings exist for GitHub-Flask:
|GITHUB_CLIENT_ID||Your GitHub application’s client id. Go to https://github.com/settings/applications to register new application.|
|GITHUB_CLIENT_SECRET||Your GitHub application’s client secret.|
|GITHUB_BASE_URL||Base URL for API requests. Override this to use with GitHub Enterprise. Default is “https://api.github.com/”.|
|GITHUB_AUTH_URL||Base authentication endpoint. Override this to use with GitHub Enterprise. Default is “https://github.com/login/oauth/”.|
Invoking Remote Methods¶
We need to register a function as a token getter for Github-Flask extension.
It will be called automatically by the extension to get the access token of
the user. It should return the access token or
@github.access_token_getter def token_getter(): user = g.user if user is not None: return user.github_access_token
@app.route('/repo') def repo(): repo_dict = github.get('repos/cenkalti/github-flask') return str(repo_dict)
A full example can be found in example.py file.
Install the required Flask-SQLAlchemy package first.
Then edit the file and change
Then you can run it as a python script:
$ pip install Flask-SQLAlchemy $ python example.py
Provides decorators for authenticating users with GitHub within a Flask application. Helper methods are also provided interacting with GitHub API.
Registers a function as the access_token getter. Must return the access_token used to make requests to GitHub on the user’s behalf.
Redirect to GitHub and request access to a user’s data.
Decorator for the route that is used as the callback for authorizing with GitHub. This callback URL can be set in the settings for the app or passed in during authorization.
post(resource, data, **kwargs)¶
request('POST', resource). Use this to make POST request since it will also encode
datato ‘application/x-www-form-urlencoded’ format.
raw_request(method, resource, params=None, **kwargs)¶
Makes a HTTP request and returns the raw
request(method, resource, all_pages=False, **kwargs)¶
Makes a request to the given endpoint. Keyword arguments are passed to the
request()method. If the content type of the response is JSON, it will be decoded automatically and a dictionary will be returned. Otherwise the
Responseobject is returned.