0
我在我的網站實施谷歌登錄,我想訪問用戶的位置,但我無法訪問。如何使用谷歌客戶端API獲取用戶位置信息
我搜查了互聯網,但無法獲得有用的信息。
驗證碼
if (!function_exists('curl_reset'))
{
function curl_reset(&$ch)
{
$ch = curl_init();
}
}
require_once __DIR__ . '/google-api-php-client-2.2.0/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$redirectURL = 'www.mysite.com/gmail-callback.php';
$client->setRedirectUri($redirectURL);
$client->addScope("email");
$client->addScope("profile");
//$client->addScope('https://www.googleapis.com/auth/glass.location');
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
回調代碼
if (!function_exists('curl_reset'))
{
function curl_reset(&$ch)
{
$ch = curl_init();
}
}
require_once __DIR__ . '/google-api-php-client-2.2.0/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
if(isset($_GET['code'])){
$client->authenticate($_GET['code']);
$_SESSION['gmail_access_token'] = $client->getAccessToken();
}elseif(!isset($_GET['code'])){
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/mysuite';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
if(!empty($_SESSION['gmail_access_token'])){
$client->setAccessToken($_SESSION['gmail_access_token']);
$service = new Google_Service_Oauth2($client);
$user = $service->userinfo->get();
print_r($user); //printing user information, but no user location
}