Authorization Flow Authentication - Ruby on Rails 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

Create a new Rails app and install the RingCentral Ruby SDK

$ rails new authorization-flow
$ cd authorization-flow
$ bundle add ringcentral-sdk

Create a new controller

$ rails generate controller main login

Browse to the "app/controllers" folder and edit the main_controller.rb. Be sure to edit the variables in <ALL CAPS> with your app credentials.

class MainController < ActionController::Base
  require 'ringcentral'

  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 = RingCentral.new(RINGCENTRAL_CLIENT_ID, RINGCENTRAL_CLIENT_SECRET, RINGCENTRAL_SERVER_URL);

  def login
    base_url = $rcsdk.authorize_uri(RINGCENTRAL_REDIRECT_URL, "initialState")
    @authorize_uri = base_url
  end

  def oauth2callback
    auth_code = params[:code]
    $rcsdk.authorize(username: nil, extension: nil, password: nil, auth_code: auth_code, redirect_uri: RINGCENTRAL_REDIRECT_URL)
    # Save tokens to session
    session[:tokens] = $rcsdk.token
    redirect_to "/main/test"
  end

  def callapis
    req = params[:api]
    api = req.split(/=/)
    if $rcsdk.token == nil
      return redirect_to "/main/login"
    end

    if api[1] == 'extension'
      resp = $rcsdk.get("/restapi/v1.0/account/~/extension")
    elsif api[1] == "extension-call-log"
      resp = $rcsdk.get("/restapi/v1.0/account/~/extension/~/call-log")
    elsif api[1] == "account-call-log"
      resp = $rcsdk.get("/restapi/v1.0/account/~/call-log")
    elsif api[0] == "logout"
      $rcsdk.revoke()
      session[:tokens] = nil
      return redirect_to "/main/login"
    else
      return redirect_to "/main/test"
    end
    render({plain: resp.body})
  end
end

Edit the routes.rb file

Browse the the "config" folder and edit the file routes.rb

Rails.application.routes.draw do
  get 'main/login'
  get 'main/test'
  match '/main/:api' => 'main#callapis', via: :get
  match '/oauth2callback' => 'main#oauth2callback', via: :get
end

Implement a login page

Browse to the "app/views/main" folder and edit the login.html.erb.

<!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>

Implement a test page

$ rails generate controller main test

Do not overwrite the main_controller.rb

Answer "no" to the overwrite main_controller.rb confirmation!

Browse to the "app/views/main" folder and edit the test.html.erb.

<!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="api=extension">Read Extension Info</a></li>
      <li><a href="api=extension-call-log">Read Extension Call Log</a></li>
      <li><a href="api=account-call-log">Read Account Call Log</a></li>
  </ul>
</body>
</html>

Run Your Code

You are almost done. Now run your script.

$ bin/rails server -p 5000

Open a Web browser and load localhost:5000/main/login

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.