OAuth2 integration using Python | Sequencing.com

Real-Time Personalization (+RTP) API Guide

Here we will show how to quickly integrate Sequencing OAuth2 harnesses into your Python based environment.
 

Complete working example of a project that has all following steps done you can find in our GitHub repository.
You can just clone it and by modifying OAuth2 related settings specified below get working project quickly. 

We have also deployed this code so you can test it in action here.


There are several prerequisites in order to complete the integration:

  • you need to have a Sequencing account
  • you need to have an OAuth2 application secret
  • you need to have at least environment with Python 2.7

In order to test integration follow instructions below

1) Visit https://github.com/SequencingDOTcom/OAuth2-code-with-demo repository

2) Clone repository locally to your environment

git clone https://github.com/SequencingDOTcom/OAuth2-code-with-demo.git

3) Copy Python resources to your folder

$ mkdir /opt/oauth-site
$ cp OAuth2-code-with-demo/python-django/* /opt/oauth-site

4) Set up virtual environment and setup prerequisites

$ cd /opt/oauth-site
$ apt-get install python-virtualenv
$ virtualenv myvenv
$ source myvenv/bin/activate
$ pip install -r requirements.txt

5) Edit URL mapping in oauth2demo/urls.py, so urlpatterns look as follows

urlpatterns = [
    url(r'^$', views.auth),
    url(r'^sequencing/authorize', views.auth_callback),
    url(r'^sequencing/authorization-approved', views.api, name="api"),
]

6) Now, edit configuration settings in oauth2demo/appconfig.py so to make them conform your OAuth2 application settings. Make sure that application name, secret and redirect URL matches. 
OAuth2 management interface is available in the developer center under "Manage OAuth2 secrets" section.

For our test feedme application mentioned the section and looking as shown below

appconfig.py should be changed as follows 

Source appconfig.py snippet Changed appconfig.py snippet
"""
URI of Sequencing oAuth2 where you can request user to authorize your app.
"""
oauth2_authorization_uri = 'https://sequencing.com/oauth2/authorize'
 
"""
Sequencing API endpoint.
"""
api_uri = 'https://api.sequencing.com'
 
"""
Redirect URI of your oauth2 app, where it expects Sequencing oAuth2 to
redirect browser.
"""
redirect_uri = 'https://python-oauth-demo.sequencing.com/Default/Authcallback'
 
"""
Id of your oauth2 app (oauth2 client).
You will be able to get this value from Sequencing website.
"""
client_id = 'oAuth2 Demo Python'
 
"""
Secret of your oauth2 app (oauth2 client).
You will be able to get this value from Sequencing website.
Keep this value private.
 
"""
client_secret = 'cyqZOLZfVET_EsKv3f1xekpqe8FZDlG2rNwK5JZyMFkRisKpNC1s-IlM3hj6KlE4e2SsYRDM903Mj2T699fBCw'
 
"""
oAuth2 state.
It should be some random generated string. State you sent to authorize URI
must match the state you get, when browser is redirected to the redirect URI you provided.
"""
state = '900150983cd24fb0d6963f7d28e17f72'
"""
URI of Sequencing oAuth2 where you can request user to authorize your app.
"""
oauth2_authorization_uri = 'https://sequencing.com/oauth2/authorize'
 
"""
Sequencing API endpoint.
"""
api_uri = 'https://api.sequencing.com'
 
"""
Redirect URI of your oauth2 app, where it expects Sequencing oAuth2 to
redirect browser.
"""
redirect_uri = 'http://feedme.com/sequencing/authorize'
 
"""
Id of your oauth2 app (oauth2 client).
You will be able to get this value from Sequencing website.
"""
client_id = 'feedme'
 
"""
Secret of your oauth2 app (oauth2 client).
You will be able to get this value from Sequencing website.
Keep this value private.
"""
client_secret = 'RCGK_tcZliZw2z5BqCSr_r-psBTTGBqji_WWEyjNZnp8-eQ1hgle5d1IS_of_U7wkshNvCqXBs25B6Q7JL1EBA'
 
"""
oAuth2 state.
It should be some random generated string. State you sent to authorize URI
must match the state you get, when browser is redirected to the redirect URI you provided.
"""
state = '900150983cd24fb0d6963f7d28e17f72'

 

7) Run server

sudo /myvenv/bin/python manage.py runserver 80

 

8) Point your browse to http://feedme.com/sequencing/authorize
It will trigger redirect to Sequencing authorization page

After entering credentials you will be presented with access approval which contains application details

After clicking on "Yes (I authorize access)" result page will render list of sample files available in your account.

From the code perspective you need to note several important variables that are needed to communicate with Sequencing backend. Those are access_token and refresh_token that are obtained on lines 75-76 of oauthclient.py

We recommend storing them in user session and keeping safe.