ds08541 2017-06-21 10:11
浏览 48
已采纳

Google Analytics PHP API - 获取用户电子邮件

When authenticating in google for google Analytics I want to get the user's email id as well is it possible to do it so how can I solve this

Below is the code I have used to authenticate and save the access_token

      $scope =implode(' ', array(Google_Service_Calendar::CALENDAR_READONLY,Google_Service_Analytics::ANALYTICS_READONLY));

    $this->client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google/callback');
    $this->client->addScope($scope);
    $this->client->setAccessType("offline");

    // Handle authorization flow from the server.
    if (!isset($_GET['code'])) {
        $auth_url = $this->client->createAuthUrl();
        header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    } else {
        $this->client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $this->client->getAccessToken();

        if (isset($_SESSION['access_token']['refresh_token'])) {

            $this->googledbsave_model->create_google_cred($_SESSION['id'], $_SESSION['access_token']);


           if($_SESSION['id']==19){

                $access_token =  $_SESSION['access_token'];

                $update_token = array(

                    'access_token' => $access_token['access_token'],
                    'token_type' => $access_token['token_type'],
                    'expires_in' => $access_token['expires_in'],
                    'created' => $access_token['created']
                );

                $get_prof = $this->googledbsave_model->update_subadmin($_SESSION['id'], $update_token);

            }


        } else {
            $this->googledbsave_model->create_google_cred($_SESSION['id'], $_SESSION['access_token']);
            $this->revokeToken();

            $_SESSION['has_error'] = 1;
            $_SESSION['error_message'] = "Cannot Syncronise Account Properly... Please Authenticate Again";
        }


        $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google/getProfileIDs';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  • 写回答

1条回答 默认 最新

  • doushi5752 2017-06-26 06:11
    关注

    You are authenticating with the Google Analytics API the easiest way would be to go though the management protocol. Do an account summaries list

    $accounts = $analytics->management_accountSummaries->listManagementAccountSummaries();
    

    Technically this method is used to list the accounts the current authenticated user has access to. However it has a little bonus in that it also returns their email address in the username field.

    {  
       "kind":"analytics#accountSummaries",
       "username":"xxxx@gmail.com",
       "totalResults":14,
       "startIndex":1,
       "itemsPerPage":1000,
       "items":[  
          {      
           ....
          }]
    }
    

    You can do the same with the Google calendar API which you are also using. By doing a calendar get on the primary calendar

    $calendar = $service->calendars->get('primary');
    

    The primary calendar is the main calendar for all users.

    {
     "kind": "calendar#calendar",
     "etag": "\"E756z8zuickcYzaOnj8krCN4-Pk\"",
     "id": "xxxx@gmail.com",
     "summary": "laurly71@gmail.com",
     "timeZone": "Europe/Copenhagen"
    }
    

    A lot of the Google APIs have this hidden feature you just have to figure out which method you need to call to get the information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示