In addition to the previous code sample, I wrote a script to demonstrate SC4SB using OAuth 2.0 ... get it - Part 2.0 ;)
This sample is also slightly different in that it uses curl to better demonstrate the process:
<?php
// to understand OAuth process see
// http://www-10.lotus.com/ldd/appdevwiki.nsf/xpDocViewer.xsp?lookupName=
// IBM+Social+Business+Toolkit+documentation#action=openDocument&res_title=
// OAuth_2.0_APIs_for_web_server_flow_sbt&content=pdcontent
// Smart Cloud URL (test server listed)
$sc4sbUrl = 'https://apps.na.collabservtest.lotus.com';
$sc4sbUrlAuth = $sc4sbUrl . '/manage/oauth2/authorize';
$sc4sbUrlToken = $sc4sbUrl . '/manage/oauth2/token';
// vendor application (must be SSL for callback to work)
$callbackUrl = 'https://localhost/SC4SBOAuth2_0.php';
// curl options
// note that the SSL certificate is NOT being verified
$options = array(CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true);
// Step 1: Register the application
$clientId = '<client ID>';
$clientSecret = '<client secret>';
// check if a code token is available or an error exists
$error = getUrlParam($_SERVER['REQUEST_URI'], 'oauth_error');
$code = getUrlParam($_SERVER['REQUEST_URI'], 'code');
if($error == NULL && $code == NULL){
// Step 2: Obtain authorization code
$url = $sc4sbUrlAuth . '?';
$url .= 'response_type=code';
$url .= '&callback_uri=' . $callbackUrl;
$url .= '&client_id=' . $clientId;
header('Location: ' . $url);
exit;
// the result is SC4SB returning to the callbackUrl
} else {
if($error == NULL){
// Step 3: Exchange authorization code for access and refresh tokens
$auth = array('Authorization: OAuth callback_uri="' . $callbackUrl . '", client_secret="' . $clientSecret . '", client_id="' . $clientId . '", grant_type="authorization_code", code="' . $code . '"');
$ch = curl_init($sc4sbUrlToken);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth);
// if the result if false, check curl_error($ch);
$result = curl_exec($ch);
// If the request is successful, the following parameters are returned in the body of the response with an HTTP response code of 200:
// refresh_token, access_token, issued_on, expires_in, token_type
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
parse_str($body);
curl_close($ch);
// you can store the refresh token to request a new access token in the future
// e.g. for up to 90 days
// Step 4: Use the access token to allow API access
// access_token is assigned from the parse_str function
$bearer = array('Authorization: Bearer ' . $access_token);
$ch = curl_init($sc4sbUrl . '/api/bss/resource/customer');
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $bearer);
print curl_exec($ch);
curl_close($ch);
} else {
print $error;
}
}
function getUrlParam($url, $name){
parse_str(parse_url($url, PHP_URL_QUERY), $params);
return isset($params[$name]) ? $params[$name] : null;
}
?>