1

当谷歌认证的谷歌Analytics(分析)我想获得用户的电子邮件ID,以及是否有可能做到这一点,所以我怎么能解决这个谷歌Analytics(分析)PHP API - 获取用户电子邮件

下面是我的代码用于验证并保存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)); 
+0

到目前为止你做了什么?没有看到我们无法帮助你。请分享一些你写的代码 –

+0

我已经更新了这个问题 – Fawaz

回答

1

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

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

技术上使用这种方法列出当前认证的用户具有访问权限的帐户。不过它有一点小小的好处,它还会在用户名字段中返回他们的电子邮件地址。

{ 
    "kind":"analytics#accountSummaries", 
    "username":"[email protected]", 
    "totalResults":14, 
    "startIndex":1, 
    "itemsPerPage":1000, 
    "items":[ 
     {  
     .... 
     }] 
} 

您也可以使用您也在使用的Google日历API。通过在主日历

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

calendar get主日历是所有用户的主日历。

{ 
"kind": "calendar#calendar", 
"etag": "\"E756z8zuickcYzaOnj8krCN4-Pk\"", 
"id": "[email protected]", 
"summary": "[email protected]", 
"timeZone": "Europe/Copenhagen" 
} 

很多Google API都有这个隐藏功能,您只需要弄清楚需要调用哪种方法才能获取信息。

相关问题