Skip to content

Commit cff893c

Browse files
committed
authorize_url bugfix
1 parent 43cff51 commit cff893c

File tree

13 files changed

+132
-116
lines changed

13 files changed

+132
-116
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
CHANGELOG
22
---------
3+
- **2017-06-20**: 2.2.1
4+
- Bugfixes
5+
- Fix `client.authorize_url`
6+
- Fix OAuth2 Sintra demo
37
- **2017-06-05**: 2.2.0
48
- Additions
59
- Generic Multipart helper

lib/ringcentral_sdk.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RingCentralSdk is a SDK for the RingCentral REST API
22
module RingCentralSdk
3-
VERSION = '2.2.0'.freeze
3+
VERSION = '2.2.1'.freeze
44

55
RC_SERVER_PRODUCTION = 'https://platform.ringcentral.com'.freeze
66
RC_SERVER_SANDBOX = 'https://platform.devtest.ringcentral.com'.freeze

lib/ringcentral_sdk/rest/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def new_oauth2_client
161161
@config.app_key,
162162
@config.app_secret,
163163
site: @config.server_url,
164-
authorize_url: AUTHZ_ENDPOINT,
164+
authorize_url: @config.authorize_url,
165165
token_url: TOKEN_ENDPOINT
166166
)
167167
end

lib/ringcentral_sdk/rest/configuration.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require 'dotenv'
2+
require 'fiddle'
23
require 'logger'
34
require 'multi_json'
5+
require 'ringcentral_sdk'
6+
require 'uri'
47

58
module RingCentralSdk
69
module REST
@@ -96,6 +99,12 @@ def inflate_token
9699
@token = MultiJson.decode @token
97100
end
98101
end
102+
103+
def authorize_url
104+
puts @server_url
105+
puts RingCentralSdk::REST::Client::AUTHZ_ENDPOINT
106+
URI.join(@server_url, RingCentralSdk::REST::Client::AUTHZ_ENDPOINT)
107+
end
99108
end
100109
end
101110
end

ringcentral_sdk.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ version = $1
77
Gem::Specification.new do |s|
88
s.name = lib
99
s.version = version
10-
s.date = '2017-04-01'
10+
s.date = '2017-06-20'
1111
s.summary = 'RingCentral SDK - Ruby SDK for the RingCentral Connect Platform API'
1212
s.description = 'A Ruby SDK for the RingCentral Connect Platform API'
1313
s.authors = ['John Wang']
1414
s.email = '[email protected]'
15-
s.homepage = 'https://github.com/grokify/'
15+
s.homepage = 'https://github.com/grokify/ringcentral-sdk-ruby'
1616
s.licenses = ['MIT']
1717
s.files = Dir['lib/**/**/*']
1818
s.files += Dir['[A-Z]*'] + Dir['test/**/*']

scripts/oauth2-sinatra/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source ENV['GEM_SOURCE'] || "https://rubygems.org"
22

33
gem 'ringcentral_sdk'
4-
gem 'sinatra'
4+
gem 'sinatra'

scripts/oauth2-sinatra/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ RingCentral 3-Legged OAuth Demo in Ruby
33

44
## Overview
55

6-
This is a quick 3-legged OAuth demo that runs using Sinatra.
6+
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.
77

88
## Installation
99

1010
### Via Bundler
1111

1212
```bash
13-
$ git clone https://github.com/grokify/ringcentral-sdk-ruby
14-
$ cd ringcentral-sdk-ruby/scripts/oauth2-sinatra
13+
$ git clone https://github.com/grokify/ringcentral-demos-oauth
14+
$ cd ringcentral-oauth-demos/ruby-sinatra
1515
$ bundle
1616
```
1717

@@ -20,24 +20,37 @@ $ bundle
2020
```bash
2121
$ gem install ringcentral_sdk
2222
$ gem install sinatra
23-
$ git clone https://github.com/grokify/ringcentral-sdk-ruby
23+
$ git clone https://github.com/grokify/ringcentral-demos-oauth
2424
```
2525

2626
## Configuration
2727

28-
Edit the `oauth2.rb` file to add your application key and application secret.
28+
Edit the `.env` file to add your application key and application secret.
2929

3030
```bash
31-
$ cd ringcentral-sdk-ruby/scripts/oauth2-sinatra
32-
$ vi oauth2.rb
31+
$ cd ringcentral-demos-oauth/ruby-sinatra
32+
$ cp config-sample.env.txt .env
33+
$ vi .env
3334
```
3435

35-
## Running the Demo
36+
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:
37+
38+
```
39+
http://localhost:8080/callback
40+
```
41+
42+
## Usage
3643

3744
Open the web page:
3845

3946
```bash
40-
$ ruby oauth2.rb
47+
$ ruby app.rb
48+
```
49+
50+
Go to the URL:
51+
4152
```
53+
http://localhost:8080
54+
````
4255
43-
Then click the <input type="button" value="Login with RingCentral"> button to authorize the demo app and view the access token.
56+
Then click the <input type="button" value="Login with RingCentral"> button to authorize the demo app and view the access token.

scripts/oauth2-sinatra/app.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!ruby
2+
3+
require 'sinatra'
4+
require 'multi_json'
5+
require 'ringcentral_sdk'
6+
7+
# Create and edit the .env file:
8+
# $ cp config-sample.env.txt .env
9+
10+
client = RingCentralSdk::REST::Client.new do |config|
11+
config.load_env = true
12+
end
13+
14+
set :logger, Logger.new(STDOUT)
15+
set :port, ENV['MY_APP_PORT']
16+
17+
get '/' do
18+
token_json = client.token.nil? \
19+
? '' : MultiJson.encode(client.token.to_hash, pretty: true)
20+
21+
state = rand(1000000)
22+
logger.info("OAuth2 Callback Request State #{state}")
23+
24+
erb :index, locals: {
25+
authorize_uri: client.authorize_url(state: state),
26+
redirect_uri: client.config.redirect_url,
27+
token_json: token_json}
28+
end
29+
30+
get '/callback' do
31+
code = params.key?('code') ? params['code'] : ''
32+
state = params.key?('state') ? params['state'] : ''
33+
logger.info("OAuth2 Callback Response State #{state}")
34+
token = client.authorize_code(code) if code
35+
''
36+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RC_APP_KEY=myAppKey
2+
RC_APP_SECRET=myAppSecret
3+
RC_SERVER_URL=https://platform.devtest.ringcentral.com
4+
RC_APP_REDIRECT_URL=http://localhost:8080/callback
5+
MY_APP_HOST=localhost
6+
MY_APP_PORT=8080

scripts/oauth2-sinatra/oauth2.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

scripts/oauth2-sinatra/rc_config_sample.env.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="UTF-8">
5-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
6-
<script>
7-
var auth_url = '<%= authorize_url %>';
8-
9-
$.oauthpopup = function(options)
10-
{
11-
options.windowName = options.windowName || 'ConnectWithOAuth'; // should not include space for IE
12-
options.windowOptions = options.windowOptions || 'location=0,status=0,width=400,height=660';
13-
options.callback = options.callback || function(){ window.location.reload(); };
14-
var that = this;
15-
console.log(options.path);
16-
that._oauthWindow = window.open(options.path, options.windowName, options.windowOptions);
17-
that._oauthInterval = window.setInterval(function(){
18-
if (that._oauthWindow.closed) {
19-
window.clearInterval(that._oauthInterval);
20-
options.callback();
21-
}
22-
}, 1000);
23-
};
24-
25-
function Auth3l(rcDemo) {
26-
var t=this;
27-
t.authorize = function() {
28-
var urltoopen = '<%= authorize_url %>';
29-
$.oauthpopup({
30-
path: urltoopen,
31-
callback: function()
32-
{
33-
console.log('callback');
3+
<head>
4+
<meta charset="UTF-8">
5+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
6+
<script>
7+
8+
var config = {
9+
authUri: '<%= authorize_uri %>',
10+
redirectUri: '<%= redirect_uri %>',
11+
}
12+
13+
var OAuthCode = function(config) {
14+
this.config = config;
15+
this.loginPopup = function() {
16+
this.loginPopupUri(this.config['authUri'], this.config['redirectUri']);
17+
}
18+
this.loginPopupUri = function(authUri, redirectUri) {
19+
var win = window.open(authUri, 'windowname1', 'width=800, height=600');
20+
21+
var pollOAuth = window.setInterval(function() {
22+
try {
23+
console.log(win.document.URL);
24+
if (win.document.URL.indexOf(redirectUri) != -1) {
25+
window.clearInterval(pollOAuth);
26+
win.close();
27+
location.reload();
28+
}
29+
} catch(e) {
30+
console.log(e)
3431
}
35-
});
32+
}, 100);
3633
}
3734
}
3835

39-
var oauth = new Auth3l();
40-
41-
</script>
42-
</head>
43-
<body>
44-
<h1>3-Legged OAuth 2.0 Demo</h1>
45-
<p>To run this demo use the following command:</p>
46-
<p>$ ruby oauth.rb</p>
47-
<p><input type="button" onclick="oauth.authorize()" value="Login with RingCentral"></p>
48-
<p>App Key: <%= app_key %></p>
49-
<p>Authorization URL: <%= authorize_url %></p>
50-
<p>Redirect URL: <%= redirect_uri %></p>
51-
</body>
52-
</html>
36+
var oauth = new OAuthCode(config);
37+
38+
</script>
39+
</head>
40+
<body>
41+
<h1>RingCentral 3-Legged OAuth Demo in Ruby</h1>
42+
43+
<p><input type="button" onclick="oauth.loginPopup()" value="Login with RingCentral"></p>
44+
45+
<p>Access Token</p>
46+
<pre style="background-color:#efefef;padding:1em;overflow-x:scroll"><%= token_json %></pre>
47+
48+
<p>More info:</p>
49+
<ul>
50+
<li><a href="https://developer.ringcentral.com/api-docs/latest/index.html#!#AuthorizationCodeFlow">RingCentral API Developer Guide</a></li>
51+
<li><a href="https://github.com/grokify/ringcentral-oauth-demos">GitHub repo</a>
52+
<li><a href="https://github.com/grokify/ringcentral-oauth-demos/tree/master/ruby-sinatra">GitHub repo Ruby README</a></p>
53+
</ul>
54+
</body>
55+
</html>

scripts/oauth2-sinatra/views/oauth.erb

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)