Created
June 29, 2020 01:49
-
-
Save rwaddin/09821609f567bd786e73915dcea4c83c to your computer and use it in GitHub Desktop.
Cara menggunakan token google dengan offline, di google dev console buat oaut 2 API untuk versi desktop. Kalau oauth versi web setelah login google akan diredirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* check ke goole apakah sudah pernah login | |
* init path ke secret & auth penggunaan offline apps | |
*/ | |
public function ajx_checkAuth() | |
{ | |
$client = new Google_Client(); | |
$client->setApplicationName('MyAPP'); | |
$client->setScopes([ | |
"https://www.google.com/m8/feeds", | |
"https://www.googleapis.com/auth/contacts" | |
]); | |
$client->setAuthConfig($this->config->item("path_secret")); | |
$client->setAccessType('offline'); | |
$client->setPrompt('select_account consent'); | |
$tokenPath = $this->config->item("path_token"); | |
if (file_exists($tokenPath)) { | |
$accessToken = json_decode(file_get_contents($tokenPath), true); | |
$client->setAccessToken($accessToken); | |
} | |
// If there is no previous token or it's expired. | |
if ($client->isAccessTokenExpired()) { | |
// Refresh the token if possible, else fetch a new one. | |
if ($client->getRefreshToken()) { | |
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); | |
echo json_encode(array("status"=>true)) ; | |
} else { | |
// Request authorization from the user. | |
$url = $client->createAuthUrl(); | |
echo json_encode(array("url"=>trim($url),"status"=>false)) ; | |
} | |
}else{ | |
echo json_encode(array("status"=>true)) ; | |
} | |
} | |
public function ajx_verifyAuth() | |
{ | |
$client = new Google_Client(); | |
$client->setApplicationName('MyAPP'); | |
$client->setScopes([ | |
"https://www.google.com/m8/feeds", | |
"https://www.googleapis.com/auth/contacts" | |
]); | |
$client->setAuthConfig($this->config->item("path_secret")); | |
$client->setAccessType('offline'); | |
$client->setPrompt('select_account consent'); | |
$tokenPath = $this->config->item("path_token"); | |
if (file_exists($tokenPath)) { | |
$accessToken = json_decode(file_get_contents($tokenPath), true); | |
$client->setAccessToken($accessToken); | |
} | |
// If there is no previous token or it's expired. | |
if ($client->isAccessTokenExpired()) { | |
// Refresh the token if possible, else fetch a new one. | |
if ($client->getRefreshToken()) { | |
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); | |
} else { | |
// Request authorization from the user. | |
// Exchange authorization code for an access token. | |
$authCode = $this->input->post("code"); | |
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode); | |
$client->setAccessToken($accessToken); | |
// Check to see if there was an error. | |
if (array_key_exists('error', $accessToken)) { | |
throw new Exception(join(', ', $accessToken)); | |
} | |
} | |
// Save the token to a file. | |
if (!file_exists(dirname($tokenPath))) { | |
mkdir(dirname($tokenPath), 0700, true); | |
} | |
file_put_contents($tokenPath, json_encode($client->getAccessToken())); | |
} | |
return $client; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment