Hello All,
I want to make one API which get articles from Database.
But the twist is I want to use oAuth with this request.
Flow :
First of all existing user login with username and password.
Using Joomla default User Authentication method the given username and password need to check if the username and password is correct then oAuth Authorization (By default its should be true) take place.
Using Authorization code we can get AccessToken and which is need to pass on each and every request.
Here is the code.
public function login()
{
try
{
$credentials = Array('username' => "$_POST[txtUsername]", 'password' => "$_POST[txtPassword]" );
/* Joomla Default Authentication Method */
$login_site =& JFactory::getApplication('site');
$data['result']=$login_site->login($credentials, $options=array());
// If user successfully login then if condition is true.
if($data['result'])
{
// include our OAuth2 Server object
require_once JPATH_COMPONENT.'/helpers/server.php';
$_POST['grant_type'] = 'client_credentials';
$_POST['client_id'] = 'articles';
$_POST['client_secret'] ='articlespass';
$request = OAuth2_Request::createFromGlobals();
$response = new OAuth2_Response();
// validate the authorize request
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
$is_authorized = true;
$server->handleAuthorizeRequest($request, $response, $is_authorized);
if ($is_authorized)
{
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
}
$data = $server->handleTokenRequest($request, $response);
print_r($data);
}
}
catch (Exception $e)
{
$result_message = $e->getMessage();
echo '{got error}'. $result_message;
}
}
By Printing Data i got this result:
OAuth2_Response Object ( [version] => 1.1 [statusCode:protected] => 200 [statusText:protected] => OK [parameters:protected] => Array ( [access_token] => 546e1e60f824c753d8df97fd6e89ed45ccfc2492 [expires_in] => 3600 [token_type] => bearer [scope] => )
[httpHeaders:protected] => Array ( [Location] => http://www.example.com/index.php?code=ea1c1e0dab5b2aa667210fb33cba6615cf00d5c7&state=xyz [Cache-Control] => no-store [Pragma] => no-cache )
)
But problem is how can i access this ACCESS_TOKEN ?
Please Help. You have a chance to earn some money.