dongliugu8843 2017-07-31 07:19
浏览 51

如何使用谷歌客户端API获取用户位置信息

I implementing google login in my site, I want to access user's location but I am not able to access.

I have searched the internet but could not get useful info.

authentication code

    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));

callback code

    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
    }
  • 写回答

1条回答 默认 最新

  • dtfpznrbn503027700 2017-07-31 09:33
    关注

    You can use google latitude service. (Google_LatitudeService.php)

    Sample Code (given by google):

    <?php
    /*
     * Copyright 2011 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    session_start();
    
    require_once '../../src/Google_Client.php';
    require_once '../../src/contrib/Google_LatitudeService.php';
    
    $client = new Google_Client();
    // Visit https://code.google.com/apis/console to generate your
    // oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
    // $client->setClientId('insert_your_oauth2_client_id');
    // $client->setClientSecret('insert_your_oauth2_client_secret');
    // $client->setRedirectUri('insert_your_oauth2_redirect_uri');
    $client->setApplicationName("Latitude_Example_App");
    $service = new Google_LatitudeService($client);
    
    if (isset($_REQUEST['logout'])) {
      unset($_SESSION['access_token']);
    }
    
    if (isset($_GET['code'])) {
      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();
      $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
      header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }
    
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
      $client->setAccessToken($_SESSION['access_token']);
    } else {
      $authUrl = $client->createAuthUrl();
    }
    
    if ($client->getAccessToken()) {
      // Start to make API requests.
      //$location = $service->location->listLocation();
      $currentLocation = $service->currentLocation->get();
      $_SESSION['access_token'] = $client->getAccessToken();
    }
    ?>
    <!doctype html>
    <html>
    <head><link rel='stylesheet' href='style.css' /></head>
    <body>
    <header><h1>Google Latitude Sample App</h1></header>
    <div class="box">
      <?php if(isset($currentLocation)): ?>
        <div class="currentLocation">
          <pre><?php var_dump($currentLocation); ?></pre>
        </div>
      <?php endif ?>
    
      <?php if (isset($location)): ?>
        <div class="location">
          <pre><?php var_dump($location); ?></pre>
        </div>
      <?php endif ?>
    
      <?php
        if(isset($authUrl)) {
          print "<a class='login' href='$authUrl'>Connect Me!</a>";
        } else {
         print "<a class='logout' href='?logout'>Logout</a>";
        }
      ?>
    </div>
    </body></html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图