PHP Classes

Oauth access token into database

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How to Implement a PH...  >  All threads  >  Oauth access token into database  >  (Un) Subscribe thread alerts  
Subject:Oauth access token into database
Summary:saving the access token and reusing it without asking for login
Messages:46
Author:Prabhulal Ramesh
Date:2014-11-20 12:59:04
 
  1 - 10   11 - 20   21 - 30   31 - 40   41 - 46  

  11. Re: Oauth access token into database   Reply   Report abuse  
Picture of Prabhulal Ramesh Prabhulal Ramesh - 2014-11-21 05:29:31 - In reply to message 10 from Justin
Justin , i am not clear about the flow ,

1) like say the database is empty for the first time, so what function should i call for the first time and new user and waht are the paramerters to be passed in that function. where should i check if the user has access keys or not ??

2) and what function or class to be used when the user is already authenticated .... and have the keys... which function should i use to make a share article like getaccesstoken ??

Please guys just help me sort this out.

  12. Re: Oauth access token into database   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-11-21 05:42:53 - In reply to message 8 from Prabhulal Ramesh
MySQLi is just a newer API to access MySQL introduced in PHP 5. The old MySQL API will be removed in PHP 7. Anyway, it does not matter unless you are using a PHP version older than PHP 5.

  13. Re: Oauth access token into database   Reply   Report abuse  
Picture of Prabhulal Ramesh Prabhulal Ramesh - 2014-11-21 05:47:12 - In reply to message 12 from Manuel Lemos
Thank you Lemos for Clarifying me on that.

So what should i do when i dont have anything in database , how will the entry be added ??? for example i am getting this error when running this page mysqli_offline_access_to_google.php.... i have changed the parameters to LinkedIn


  14. Re: Oauth access token into database   Reply   Report abuse  
Picture of Justin Justin - 2014-11-21 05:47:48 - In reply to message 11 from Prabhulal Ramesh
Take a look at this, perhaps it might help.

require_once(__DIR__ .'/../oauth/http.php');
require_once(__DIR__ .'/../oauth/oauth_client.php');
require_once(__DIR__ .'/../oauth/database_oauth_client.php');
require_once(__DIR__ .'/../oauth/mysqli_oauth_client.php');
$client = new mysqli_oauth_client_class;


$client->database = [
'host' => 'localhost',
'user' => '', # <<< SET THIS
'password'=> '', # <<< SET THIS
'name' => '', # <<< SET THIS
'port' => 0,
'socket' => '/var/lib/mysql/mysql.sock'
];
$client->server = 'Google'; # <<< LinkedIn ?
$client->client_id = ''; # <<< SET THIS
$client->client_secret = ''; # <<< SET THIS
$client->scope = ''; # <<< SET THIS
$client->offline = true; # <<< BECAUSE WE SET THIS TO OFFLINE, IT WILL GET THE CORRECT TOKEN, AUTOMATICALLY
$client->debug = true;
$client->debug_http = true;
$client->authorization_header = true;
$client->redirect_uri = filter_var('http://'. $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);

# SET THESE FOR YOUR API CALL
$userId = ""; # <<< Used for in your APP, perhaps a $_SESSION ?
$urlToCall = "";
$method = "";
$params = [];
$options = [];

# START THE CALL
if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->authorization_error))
{
$client->error = $client->authorization_error;
$success = false;
}
elseif(strlen($client->access_token))
{
if($data['method'] === 'POST' || $data['method'] === 'PUT') {
if(!array_key_exists('RequestContentType', $data)) {
$data['options']['RequestContentType'] = 'application/json';
}
}

$success = $client->CallAPI($urlToCall, $method, $params, $options, $call);

if($success)
$success = $client->SetUser($userId); # <<< HERE IS WHERE THE DATABASE WILL BE SET.

}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;
if($success)
{
echo "SUCCESSFUL CALL";
var_dump($call); # <<< THIS IS THE CALL RESPONSE From $client->CallAPI
}
else
{
echo "ERROR ". HtmlSpecialChars($client->error);
}

  15. Re: Oauth access token into database   Reply   Report abuse  
Picture of Prabhulal Ramesh Prabhulal Ramesh - 2014-11-21 05:49:03 - In reply to message 13 from Prabhulal Ramesh
Sorry forgot to post error ... the error is ..."Error: it was not found the OAuth session for user 1"

  16. Re: Oauth access token into database   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-11-21 05:49:55 - In reply to message 9 from Prabhulal Ramesh
The example mysqli_login_with_google.php has a call named SetUser that associates one user of your system with a given ID with that OAuth session.

So you are supposed to have a system of users in some table of yours. Each user has an id number.

In practice you could check in your users table if there is an user with a given email in your system retrieved from Google (LinkedIn in your case).

If there is an user with that email address, use its ID and pass it to SetUser, so the OAuth session is associated to that user.

If there is no user with that email address in your users table, create a new record with all the details you need retrieved from LinkedIn (email, name, etc..) and then use the new user id to pass to SetUser.

Got it?

  17. Re: Oauth access token into database   Reply   Report abuse  
Picture of Prabhulal Ramesh Prabhulal Ramesh - 2014-11-21 06:14:15 - In reply to message 14 from Justin
Dear Justin and Lemos Thank you very much for your help.

I got the token inserted in my table.


But the thing is when i delete Histroy , i was again asked to login , it is not fetching the details from database and login in automatically.

beacuse i am going to share articles from my app by using a ajax to this offline page , so i need to check if the user id is there in the table .and

if yes then share and just give a success message in json to my app,

if no then i will send a error message saying u need to login first ,

if Id is present then i will say you need to reauthenticate from settings page,

can u help on this one last thing guys.... after this i think i will complete my apps last pedning work.

  18. Re: Oauth access token into database   Reply   Report abuse  
Picture of Prabhulal Ramesh Prabhulal Ramesh - 2014-11-21 06:22:17 - In reply to message 14 from Justin

What i found is that $client->Initialize() which is there in mysqli_oauth_client.php .... connects to my database ,

but $client->Process is not found in mysqli_oauth_client ....

its present in oauth_client which uses cookies and session in think.

how can i check if the user is present in table and if yes then how to process he data ( i mean how to get his values into the success parameter)

  19. Re: Oauth access token into database   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-11-21 06:23:10 - In reply to message 17 from Prabhulal Ramesh
When you call the API offline you need to specify the same user ID that you used to retrieve the access tokens.

In the mysqli_offline_access_to_google.php example script you can see a line:

$client->user = 1;

The 1 there must be the same user you passed to the SetUser function in the script you used to retrieve the OAuth tokens.

  20. Re: Oauth access token into database   Reply   Report abuse  
Picture of Prabhulal Ramesh Prabhulal Ramesh - 2014-11-21 06:29:02 - In reply to message 19 from Manuel Lemos

so the idea is to have 2 separate pages ??

example user id = 33.

first time i am checking database for this user in my table... if yes then go with your $client->33 ....

else go with the current code where i got to insert (code given by justin).


Will my above said approach be good ??

 
  1 - 10   11 - 20   21 - 30   31 - 40   41 - 46