Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌分析PHP API-获取用户电子邮件_Php_Google Analytics_Google Api_Google Analytics Api_Google Api Php Client - Fatal编程技术网

谷歌分析PHP API-获取用户电子邮件

谷歌分析PHP API-获取用户电子邮件,php,google-analytics,google-api,google-analytics-api,google-api-php-client,Php,Google Analytics,Google Api,Google Analytics Api,Google Api Php Client,当在google中为google Analytics进行身份验证时,我也想获取用户的电子邮件id,是否有可能这样做?我如何解决这个问题 下面是我用来验证和保存访问令牌的代码 $scope =implode(' ', array(Google_Service_Calendar::CALENDAR_READONLY,Google_Service_Analytics::ANALYTICS_READONLY)); $this->client->setRedirectUr

当在google中为google Analytics进行身份验证时,我也想获取用户的电子邮件id,是否有可能这样做?我如何解决这个问题

下面是我用来验证和保存访问令牌的代码

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

您正在使用Google Analytics API进行身份验证,最简单的方法是通过管理协议。做一个

从技术上讲,此方法用于列出当前经过身份验证的用户有权访问的帐户。不过,它还有一点好处,那就是它还可以在用户名字段中返回他们的电子邮件地址

{  
   "kind":"analytics#accountSummaries",
   "username":"xxxx@gmail.com",
   "totalResults":14,
   "startIndex":1,
   "itemsPerPage":1000,
   "items":[  
      {      
       ....
      }]
}
您也可以使用Google日历API执行同样的操作。通过在主日历上执行

$calendar = $service->calendars->get('primary');
主日历是所有用户的主日历

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

很多GoogleAPI都有这个隐藏的特性,你只需要找出需要调用哪个方法来获取信息

到目前为止你做了什么?没有看到我们不能帮助你。请分享你写的代码。我已经更新了问题
{
 "kind": "calendar#calendar",
 "etag": "\"E756z8zuickcYzaOnj8krCN4-Pk\"",
 "id": "xxxx@gmail.com",
 "summary": "laurly71@gmail.com",
 "timeZone": "Europe/Copenhagen"
}