Skip to content

Commit

Permalink
authorize_url bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Jun 20, 2017
1 parent 43cff51 commit cff893c
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 116 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG
---------
- **2017-06-20**: 2.2.1
- Bugfixes
- Fix `client.authorize_url`
- Fix OAuth2 Sintra demo
- **2017-06-05**: 2.2.0
- Additions
- Generic Multipart helper
Expand Down
2 changes: 1 addition & 1 deletion lib/ringcentral_sdk.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RingCentralSdk is a SDK for the RingCentral REST API
module RingCentralSdk
VERSION = '2.2.0'.freeze
VERSION = '2.2.1'.freeze

RC_SERVER_PRODUCTION = 'https://platform.ringcentral.com'.freeze
RC_SERVER_SANDBOX = 'https://platform.devtest.ringcentral.com'.freeze
Expand Down
2 changes: 1 addition & 1 deletion lib/ringcentral_sdk/rest/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def new_oauth2_client
@config.app_key,
@config.app_secret,
site: @config.server_url,
authorize_url: AUTHZ_ENDPOINT,
authorize_url: @config.authorize_url,
token_url: TOKEN_ENDPOINT
)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/ringcentral_sdk/rest/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'dotenv'
require 'fiddle'
require 'logger'
require 'multi_json'
require 'ringcentral_sdk'
require 'uri'

module RingCentralSdk
module REST
Expand Down Expand Up @@ -96,6 +99,12 @@ def inflate_token
@token = MultiJson.decode @token
end
end

def authorize_url
puts @server_url
puts RingCentralSdk::REST::Client::AUTHZ_ENDPOINT
URI.join(@server_url, RingCentralSdk::REST::Client::AUTHZ_ENDPOINT)
end
end
end
end
4 changes: 2 additions & 2 deletions ringcentral_sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ version = $1
Gem::Specification.new do |s|
s.name = lib
s.version = version
s.date = '2017-04-01'
s.date = '2017-06-20'
s.summary = 'RingCentral SDK - Ruby SDK for the RingCentral Connect Platform API'
s.description = 'A Ruby SDK for the RingCentral Connect Platform API'
s.authors = ['John Wang']
s.email = 'johncwang@gmail.com'
s.homepage = 'https://github.com/grokify/'
s.homepage = 'https://github.com/grokify/ringcentral-sdk-ruby'
s.licenses = ['MIT']
s.files = Dir['lib/**/**/*']
s.files += Dir['[A-Z]*'] + Dir['test/**/*']
Expand Down
2 changes: 1 addition & 1 deletion scripts/oauth2-sinatra/Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source ENV['GEM_SOURCE'] || "https://rubygems.org"

gem 'ringcentral_sdk'
gem 'sinatra'
gem 'sinatra'
33 changes: 23 additions & 10 deletions scripts/oauth2-sinatra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ RingCentral 3-Legged OAuth Demo in Ruby

## Overview

This is a quick 3-legged OAuth demo that runs using Sinatra.
This is a quick 3-legged OAuth demo that runs using Ruby and Sinatra with the [RingCentral Ruby SDK](https://github.com/grokify/ringcentral-sdk-ruby) v1.2.1.

## Installation

### Via Bundler

```bash
$ git clone https://github.com/grokify/ringcentral-sdk-ruby
$ cd ringcentral-sdk-ruby/scripts/oauth2-sinatra
$ git clone https://github.com/grokify/ringcentral-demos-oauth
$ cd ringcentral-oauth-demos/ruby-sinatra
$ bundle
```

Expand All @@ -20,24 +20,37 @@ $ bundle
```bash
$ gem install ringcentral_sdk
$ gem install sinatra
$ git clone https://github.com/grokify/ringcentral-sdk-ruby
$ git clone https://github.com/grokify/ringcentral-demos-oauth
```

## Configuration

Edit the `oauth2.rb` file to add your application key and application secret.
Edit the `.env` file to add your application key and application secret.

```bash
$ cd ringcentral-sdk-ruby/scripts/oauth2-sinatra
$ vi oauth2.rb
$ cd ringcentral-demos-oauth/ruby-sinatra
$ cp config-sample.env.txt .env
$ vi .env
```

## Running the Demo
In the [Developer Portal](http://developer.ringcentral.com/), ensure the redirect URI in your config file has been entered in your app configuration. By default, the URL is set to the following for this demo:

```
http://localhost:8080/callback
```

## Usage

Open the web page:

```bash
$ ruby oauth2.rb
$ ruby app.rb
```

Go to the URL:

```
http://localhost:8080
````

Then click the <input type="button" value="Login with RingCentral"> button to authorize the demo app and view the access token.
Then click the <input type="button" value="Login with RingCentral"> button to authorize the demo app and view the access token.
36 changes: 36 additions & 0 deletions scripts/oauth2-sinatra/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!ruby

require 'sinatra'
require 'multi_json'
require 'ringcentral_sdk'

# Create and edit the .env file:
# $ cp config-sample.env.txt .env

client = RingCentralSdk::REST::Client.new do |config|
config.load_env = true
end

set :logger, Logger.new(STDOUT)
set :port, ENV['MY_APP_PORT']

get '/' do
token_json = client.token.nil? \
? '' : MultiJson.encode(client.token.to_hash, pretty: true)

state = rand(1000000)
logger.info("OAuth2 Callback Request State #{state}")

erb :index, locals: {
authorize_uri: client.authorize_url(state: state),
redirect_uri: client.config.redirect_url,
token_json: token_json}
end

get '/callback' do
code = params.key?('code') ? params['code'] : ''
state = params.key?('state') ? params['state'] : ''
logger.info("OAuth2 Callback Response State #{state}")
token = client.authorize_code(code) if code
''
end
6 changes: 6 additions & 0 deletions scripts/oauth2-sinatra/config-sample.env.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RC_APP_KEY=myAppKey
RC_APP_SECRET=myAppSecret
RC_SERVER_URL=https://platform.devtest.ringcentral.com
RC_APP_REDIRECT_URL=http://localhost:8080/callback
MY_APP_HOST=localhost
MY_APP_PORT=8080
32 changes: 0 additions & 32 deletions scripts/oauth2-sinatra/oauth2.rb

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/oauth2-sinatra/rc_config_sample.env.txt

This file was deleted.

95 changes: 49 additions & 46 deletions scripts/oauth2-sinatra/views/index.erb
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var auth_url = '<%= authorize_url %>';

$.oauthpopup = function(options)
{
options.windowName = options.windowName || 'ConnectWithOAuth'; // should not include space for IE
options.windowOptions = options.windowOptions || 'location=0,status=0,width=400,height=660';
options.callback = options.callback || function(){ window.location.reload(); };
var that = this;
console.log(options.path);
that._oauthWindow = window.open(options.path, options.windowName, options.windowOptions);
that._oauthInterval = window.setInterval(function(){
if (that._oauthWindow.closed) {
window.clearInterval(that._oauthInterval);
options.callback();
}
}, 1000);
};

function Auth3l(rcDemo) {
var t=this;
t.authorize = function() {
var urltoopen = '<%= authorize_url %>';
$.oauthpopup({
path: urltoopen,
callback: function()
{
console.log('callback');
<head>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>

var config = {
authUri: '<%= authorize_uri %>',
redirectUri: '<%= redirect_uri %>',
}

var OAuthCode = function(config) {
this.config = config;
this.loginPopup = function() {
this.loginPopupUri(this.config['authUri'], this.config['redirectUri']);
}
this.loginPopupUri = function(authUri, redirectUri) {
var win = window.open(authUri, 'windowname1', 'width=800, height=600');

var pollOAuth = window.setInterval(function() {
try {
console.log(win.document.URL);
if (win.document.URL.indexOf(redirectUri) != -1) {
window.clearInterval(pollOAuth);
win.close();
location.reload();
}
} catch(e) {
console.log(e)
}
});
}, 100);
}
}

var oauth = new Auth3l();

</script>
</head>
<body>
<h1>3-Legged OAuth 2.0 Demo</h1>
<p>To run this demo use the following command:</p>
<p>$ ruby oauth.rb</p>
<p><input type="button" onclick="oauth.authorize()" value="Login with RingCentral"></p>
<p>App Key: <%= app_key %></p>
<p>Authorization URL: <%= authorize_url %></p>
<p>Redirect URL: <%= redirect_uri %></p>
</body>
</html>
var oauth = new OAuthCode(config);

</script>
</head>
<body>
<h1>RingCentral 3-Legged OAuth Demo in Ruby</h1>

<p><input type="button" onclick="oauth.loginPopup()" value="Login with RingCentral"></p>

<p>Access Token</p>
<pre style="background-color:#efefef;padding:1em;overflow-x:scroll"><%= token_json %></pre>

<p>More info:</p>
<ul>
<li><a href="https://developer.ringcentral.com/api-docs/latest/index.html#!#AuthorizationCodeFlow">RingCentral API Developer Guide</a></li>
<li><a href="https://github.com/grokify/ringcentral-oauth-demos">GitHub repo</a>
<li><a href="https://github.com/grokify/ringcentral-oauth-demos/tree/master/ruby-sinatra">GitHub repo Ruby README</a></p>
</ul>
</body>
</html>
19 changes: 0 additions & 19 deletions scripts/oauth2-sinatra/views/oauth.erb

This file was deleted.

0 comments on commit cff893c

Please sign in to comment.