-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqli_offline_access_to_google.php
94 lines (83 loc) · 2.36 KB
/
mysqli_offline_access_to_google.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
* mysqli_offline_access_to_google.php
*
* @(#) $Id: mysqli_offline_access_to_google.php,v 1.3 2013/07/31 11:48:04 mlemos Exp $
*
*/
/*
* Get the http.php file from http://www.phpclasses.org/httpclient
*/
require('http.php');
require('oauth_client.php');
require('database_oauth_client.php');
require('mysqli_oauth_client.php');
/*
* Create an object of the sub-class of the OAuth client class that is
* specialized in storing and retrieving access tokens from MySQL
* databases using the mysqli extension
*
* If you use a different database, replace this class by another
* specialized in accessing that type of database
*/
$client = new mysqli_oauth_client_class;
/*
* ID of the user in the database of your application
*/
$client->user = 1;
/*
* Define options specific to your database connection
*/
$client->database = array(
'host'=>'',
'user'=>'oauth',
'password'=>'oauth',
'name'=>'oauth',
'port'=>0,
'socket'=>'/var/lib/mysql/mysql.sock'
);
$client->server = 'Google';
/*
* Set the offline access only if you need to call an API
* when the user is not present and the token may expire
*/
$client->offline = true;
$client->debug = false;
$client->debug_http = true;
$client->client_id = ''; $application_line = __LINE__;
$client->client_secret = '';
if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('Please go to Google APIs console page '.
'http://code.google.com/apis/console in the API access tab, '.
'create a new client ID, and in the line '.$application_line.
' set the client_id to Client ID and client_secret with Client Secret.');
if(($success = $client->Initialize()))
{
/*
* The call to the Process function should not be done here anymore
* because the access token will be retrieved from the database
*/
$success = $client->CallAPI(
'https://www.googleapis.com/oauth2/v1/userinfo',
'GET', array(), array('FailOnAccessError'=>true), $user);
$success = $client->Finalize($success);
}
if($client->exit)
exit;
/*
* Use this script from the command line, so no HTML output is needed.
*/
if($success)
{
if(strlen($client->access_token))
{
echo 'The user name is ', $user->name, "\n";
echo print_r($user, 1);
}
else
echo 'The access token is not available!', "\n";
}
else
echo 'Error: ', $client->error, "\n";
?>