-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGoogleClient.php
executable file
·42 lines (36 loc) · 1.42 KB
/
GoogleClient.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
class GoogleClient
{
public static function getClient()
{
$client = new \Google_Client();
$client->setApplicationName('Google Drive Egriffon');
$client->setScopes(\Google_Service_Drive::DRIVE);
$client->setAuthConfig(__DIR__.'/json/credentials.json');
$client->setAccessType('offline');
$client->setIncludeGrantedScopes(true);
// Load previously authorized credentials from a file.
$token_path = __DIR__.'/json/token.json';
if (file_exists($token_path)) {
$accessToken = json_decode(file_get_contents($token_path), true);
if(isset($accessToken['access_token'])){
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
//file_put_contents($token_path, json_encode($client->getAccessToken()));
}
// print_r($client->getAccessToken());die;
$response = ['success' => 1, 'client' => $client];
return $response;
}
}
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
$html = "Open the following link in your browser<br>";
$html .= '<a href="'.$authUrl.'">Click Here</a>';
$response = ['success' => 0, 'html' => $html];
return $response;
}
}