Skip to content

Commit

Permalink
3Legged OAuth feature using platform methods (ringcentral#17)
Browse files Browse the repository at this point in the history
* 3Legged OAuth feature using the RC-2.0.0 Pre Release candidate

* 3Legged OAuth feature using platform methods

* 3Legged OAuth feature using platform methods

* Adding Platform Object to callback.php + code indenting

* Final PHP 3Legged OAuth Demo using PHP SDK functions
  • Loading branch information
anilkumarbp authored and grokify committed Dec 6, 2016
1 parent da46fa4 commit 5ec77b6
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
php/composer.lock
php/composer.phar
php/vendor
.vscode
javascript/public/components
javascript/public/config.js
javascript-express/.env
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The following are example screenshots of the authentication and authorization pa
* [ringcentral-sdk-ruby](https://github.com/grokify/ringcentral-sdk-ruby)
* Example Code
* [ringcentral-js/demo/oauth](https://github.com/ringcentral/ringcentral-js/tree/5f5197ccb93410d732410127d54449e79ec5c64d/demo/oauth)
* [ringcentral-oauth-demos](https://github.com/grokify/ringcentral-oauth-demos) - this project
* [ringcentral-oauth-demos](https://github.com/ringcentral/ringcentral-oauth-demos) - this project
* [ringcentral-cti-demo-js](https://github.com/ringcentral/ringcentral-cti-demo-js)

## Contributing
Expand Down
58 changes: 16 additions & 42 deletions php/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,37 @@
require_once(__DIR__ . '/vendor/autoload.php');

use RingCentral\SDK\SDK;
use RingCentral\SDK\Http\HttpException;
use RingCentral\http\Response;

session_start();

// Parse the .env file
$dotenv = new Dotenv\Dotenv(getcwd());
$dotenv -> load();
$dotenv->load();


function processCode()
{
if(!isset($_GET['code'])) {

if (!isset($_GET['code'])) {
return;
}
$authCode = $_GET['code'];

$tokenUrl = $_ENV['RC_Server'] . '/restapi/oauth/token';

$values = array(
'grant_type' => 'authorization_code',
'code' => $authCode,
'redirect_uri' => $_ENV['RC_Redirect_Url']
);

$apiKey = base64_encode($_ENV['RC_AppKey'] . ':' . $_ENV['RC_AppSecret']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic' . $apiKey,
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_POST, count($values));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($values));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// Create SDK instance
$rcsdk = new SDK($_ENV['RC_AppKey'], $_ENV['RC_AppSecret'], $_ENV['RC_Server'], 'OAuth-Demo-PHP', '1.0.0');

$response = curl_exec($ch);
// Create Platform instance
$platform = $rcsdk->platform();

$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $headerSize);

//close connection
curl_close($ch);
$qs = $platform->parseAuthRedirectUrl($_SERVER['QUERY_STRING']);
$qs["redirectUri"] = $_ENV['RC_Redirect_Url'];

$body = json_encode(json_decode($body, true), JSON_PRETTY_PRINT);

//Store the response in PHP Session Object
$_SESSION['response'] = $body;

return $body;
$apiResponse = $platform->login($qs);
$_SESSION['sessionAccessToken'] = $apiResponse->text();

}

$result= processCode();

session_write_close();

?>
$result = processCode();

?>
2 changes: 1 addition & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"ringcentral/ringcentral-php": "^1.1",
"ringcentral/ringcentral-php": "2.0.0-rc1",
"vlucas/phpdotenv": "^2.2"
}
}
165 changes: 86 additions & 79 deletions php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once(__DIR__ . '/vendor/autoload.php');

use RingCentral\SDK\Http\HttpException;
use RingCentral\http\Response;
use RingCentral\SDK\Http\ApiResponse;
use RingCentral\SDK\SDK;

// Start the session
Expand All @@ -14,91 +14,98 @@
$dotenv->load();

// Create SDK instance
$rcsdk = new SDK($_ENV['RC_AppKey'],$_ENV['RC_AppSecret'],$_ENV['RC_Server'], 'OAuth-Demo-PHP', '1.0.0');
$rcsdk = new SDK($_ENV['RC_AppKey'], $_ENV['RC_AppSecret'], $_ENV['RC_Server'], 'OAuth-Demo-PHP', '1.0.0');

// Create Platform instance
$platform = $rcsdk->platform();

$url = $platform->createUrl('/restapi/oauth/authorize' . '?' .
http_build_query(
array (
'response_type' => 'code',
'redirect_uri' => $_ENV['RC_Redirect_Url'],
'client_id' => $_ENV['RC_AppKey'],
'state' => $_ENV['RC_State'],
'brand_id ' => '',
'display' => '',
'prompt' => ''
)
),
array (
'addServer' => true
)
);
// Using the authUrl to call the platform function
$url = $platform
->authUrl(array(
'redirectUri' => isset($_ENV['RC_Redirect_Url']) ? $_ENV['RC_Redirect_Url'] : '',
'state' => 'myState',
'brandId' => '',
'display' => '',
'prompt' => ''
));

// Store the url in PHP Session Objec;
$_SESSION['url'] = $url;

//set the access token using the auth object
if (isset($_SESSION['sessionAccessToken'])) {

$platform->auth()->setData((array)json_decode($_SESSION['sessionAccessToken']));
}

?>
<!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 url = '<?php echo $url; ?>';
var redirectUri = '<?php echo $_ENV['RC_Redirect_Url']; ?>';

var config = {
authUri: url,
redirectUri: redirectUri,
}

var OAuthCode = function(config) {
this.config = config;

this.loginPopup = function() {
console.log("The URL is :" + url);
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);
//win.close();
}
}, 100);

}
}

var oauth = new OAuthCode(config);

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

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

<p>After retrieving the token use the PHP SDK's auth class's set_data method to load the access_token.</p>

<p>Access Token</p>
<pre style="background-color:#efefef;padding:1em;overflow-x:scroll"><?php echo isset($_SESSION['response']) ? $_SESSION['response'] : '';?></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/python-bottle">GitHub repo Python README</a></p>
</ul>
</body>
<head>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>

var url = '<?php echo $url; ?>';
var redirectUri = '<?php echo $_ENV['RC_Redirect_Url']; ?>';

var config = {
authUri: url,
redirectUri: redirectUri,
}

var OAuthCode = function (config) {
this.config = config;

this.loginPopup = function () {
console.log("The URL is :" + url);
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);
// win.close();
}
}, 100);

}
}

var oauth = new OAuthCode(config);

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

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

<p>After retrieving the token use the PHP SDK's auth class's set_data method to load the access_token.</p>

<p>Access Token</p>
<pre>
<style="background-color:#efefef;padding:1em;overflow-x:scroll"><?php if ($platform->loggedIn()) echo json_encode($platform->auth()->data(), JSON_PRETTY_PRINT); ?></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/python-bottle">GitHub repo Python
README</a></p></li>
</ul>
</body>
</html>


Expand Down

0 comments on commit 5ec77b6

Please sign in to comment.