git clone https://github.com/google/google-api-php-client
set_include_path ( get_include_path () . PATH_SEPARATOR . APPPATH .'application/libraries/google-api-php-client/src/Google' ); require_once APPPATH . "libraries/google-api-php-client/src/Google/autoload.php"; require APPPATH . "libraries/google-api-php-client/src/Google/Client.php"; require APPPATH . "libraries/google-api-php-client/src/Google/Service/Oauth2.php"; // , Google API. $client = new Google_Client (); $client->setApplicationName ( "A3M with OAuth2 support" ); $client->setClientId ( $client_id ); $client->setClientSecret ( $client_secret ); $client->setRedirectUri ( $redirect_uri ); $client->addScope ( "email" ); $client->setOpenidRealm ( $redirect_uri ); // OpenID 2.0
// This method extracts openID2 ID form id token for backward compatibility private function getOpenIDFromToken($client, $token) { $id_token = json_decode ( $token ); $ticket = $client->verifyIdToken ( $id_token->{'id_token'} ); if ($ticket) { $data = $ticket->getAttributes (); return $data ['payload'] ['openid_id']; // user ID } return false; }
$objOAuthService = new Google_Service_Oauth2 ( $client ); if (! isset ( $authURL )) // “Expired Token” unset ( $_SESSION ['access_token'] ); // // endpoint if (isset ( $_GET ['code'] )) { $client->authenticate ( $_GET ['code'] ); $_SESSION ['access_token'] = $client->getAccessToken (); header ( 'Location: ' . filter_var ( $redirect_uri, FILTER_SANITIZE_URL ) ); } // , openid_id if (isset ( $_SESSION ['access_token'] ) && $_SESSION ['access_token']) { $client->setAccessToken ( $_SESSION ['access_token'] ); $openid_id = $this->getOpenIDFromToken ( $client, $client->getAccessToken () ); } // if ($client->getAccessToken ()) { $userData = $objOAuthService->userinfo->get (); $data ['userData'] = $userData; $_SESSION ['access_token'] = $client->getAccessToken (); $openid_id = $this->getOpenIDFromToken ( $client, $client->getAccessToken () ); } // , URI, . else { $authUrl = $client->createAuthUrl (); $data ['authUrl'] = $authUrl; header ( 'Location:' . $authUrl ); die (); // :) }
if (! $this->authentication->is_signed_in ()) { if ($userData) { $email = $userData->getEmail (); $openid_google = array('fullname' => $userData->getName (), $openid_google 'gender' => $userData->getGender (), $openid_google 'language' => $userData->getLocale (), $openid_google 'firstname' => $userData->getGivenName (), // google only $openid_google 'lastname' => $userData->getFamilyName (), // google only ); } // Store user's google data in session $this->session->set_userdata ( 'connect_create', array ( array ( 'provider' => 'openid', 'provider_id' => isset ( $openid_id ) ? $openid_id : NULL, 'email' => isset ( $email ) ? $email : NULL ), $openid_google ) ); // Create a3m account redirect ( 'account/connect_create' );
Source: https://habr.com/ru/post/266339/
All Articles