Authorization Flow Authentication - Python Quick Start

Welcome to the RingCentral Platform. RingCentral is the leading unified communications platform. From one system developers can integrate with, or build products around all the ways people communicate today: SMS, voice, fax, chat and meetings.

In this Quick Start, we are going to help you authorize a user to login with username and password to get an access token and a refresh token. Let's get started.

Create an App

The first thing we need to do is create an app in the RingCentral Developer Portal. This can be done quickly by clicking the "Create User Login App" button below. Enter a name and description if you want to change them, and click the "Create" button. If you do not yet have a RingCentral account, you will be prompted to create one.

Create User Login App

  1. Login or create an account if you have not done so already.
  2. Go to Console/Apps and click 'Create App' button.
  3. Give your app a name and description, then click Next.
  4. On the second page of the create app wizard enter the following:
    • Select 'Private' for Application Type.
    • Select 'Server/Web' for Platform Type.
  5. On the third page of the create app wizard, select the following permissions:
    • ReadAccounts,ReadCallLog
  6. Specify the redirect Uri as http://localhost:5000/oauth2callback.

When you are done, you will be taken to the app's dashboard. Make note of the Client ID and Client Secret. We will be using those momentarily.

Authorization Flow

Install RingCentral Python SDK and Flask framework

$ pip install ringcentral
$ pip install flask

Create an index.py

Create a file called index.py. Be sure to edit the variables in <ALL CAPS> with your app credentials.

from ringcentral import SDK
import urllib
from flask import Flask, render_template, request, session

app = Flask(__name__)
app.secret_key = 'somesecretstring'

RINGCENTRAL_CLIENT_ID= '<ENTER CLIENT ID>'
RINGCENTRAL_CLIENT_SECRET= '<ENTER CLIENT SECRET>'
RINGCENTRAL_SERVER_URL= 'https://platform.devtest.ringcentral.com'
RINGCENTRAL_REDIRECT_URL= 'http://localhost:5000/oauth2callback'

rcsdk = SDK(RINGCENTRAL_CLIENT_ID, RINGCENTRAL_CLIENT_SECRET, RINGCENTRAL_SERVER_URL)

@app.route('/')
@app.route('/index')
def login():
    base_url = RINGCENTRAL_SERVER_URL + '/restapi/oauth/authorize'
    params = (
        ('response_type', 'code'),
        ('redirect_uri', RINGCENTRAL_REDIRECT_URL),
        ('client_id', RINGCENTRAL_CLIENT_ID),
        ('state', 'initialState')
    )
    auth_url = base_url + '?' + urllib.urlencode(params)
    return render_template('index.html', authorize_uri=auth_url)

@app.route('/oauth2callback', methods=['GET'])
def oauth2callback():
    platform = rcsdk.platform()
    auth_code = request.values.get('code')
    platform.login('', '', '', auth_code, RINGCENTRAL_REDIRECT_URL)
    tokens = platform.auth().data()
    session['sessionAccessToken'] = tokens
    return render_template('test.html')

@app.route('/test', methods=['GET'])
def callapi():
    platform = rcsdk.platform()
    platform.auth().set_data(session['sessionAccessToken'])
    if platform.logged_in() == False:
        return login()
    api = request.values.get('api')
    if api == "extension":
        resp = platform.get("/restapi/v1.0/account/~/extension")
        return resp.response()._content
    elif api == "extension-call-log":
        resp = platform.get("/restapi/v1.0/account/~/extension/~/call-log")
        return resp.response()._content
    elif api == "account-call-log":
        resp = platform.get("/restapi/v1.0/account/~/call-log")
        return resp.response()._content
    else:
        return render_template('test.html')

@app.route('/logout', methods=['GET'])
def logout():
    platform = rcsdk.platform()
    platform.auth().set_data(session['sessionAccessToken'])
    if platform.logged_in():
        platform.logout()
    session.pop('sessionAccessToken', None)
    return login()

Create a templates folder and create an index.html and a test.html file under the templates folder

Create a file called index.html. In this file, we'll implement the login page.

<!DOCTYPE html>
<html>
  <head>
      <meta charset="UTF-8">
      <title>RingCentral Authorization Code Flow Authentication</title>
  </head>
  <body>
    <h2>
        RingCentral Authorization Code Flow Authentication
    </h2>
    <a href="{{ authorize_uri }}">Login RingCentral Account</a>
  </body>
</html>

Create a file called test.html. In this file, we'll add a few API call test cases and a logout button.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
  <b><a href="/logout">Logout</a></b>
  <h2>Call APIs</h2>
  <ul>
      <li><a href="/test?api=extension">Read Extension Info</a></li>
      <li><a href="/test?api=extension-call-log">Read Extension Call Log</a></li>
      <li><a href="/test?api=account-call-log">Read Account Call Log</a></li>
  </ul>
</body>
</html>

Run Your Code

You are almost done. Now run your script.

$ FLASK_APP=index.py flask run

Open a Web browser and load localhost:5000

Graduate Your App

Congratulations on creating your first RingCentral application. The last step is to graduate your application. We recommend going through this process for your first application so you can understand the steps to take in the future, but also to come to appreciate the care taken by RingCentral to ensure that only high-quality apps are allowed into our production environment.