谷歌分析 API 在刷新时删除会话



我必须首先说我对Google的API和文档感到非常困惑,到目前为止我已经尝试了很多不同的方法,但是确实想了解它,我终于找到了一些代码可以指导我完成它,我只是遇到了一个小问题,每次刷新页面时,我都会丢失Google的api制作的会话,我不知道为什么。

 $client = new Google_Client();
   $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
    $client->setAccessType('offline');   // Gets us our refreshtoken
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);  
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1:  The user has not authenticated we give them a link to login    
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
    }        
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
    print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
    $client->setAccessToken($_SESSION['token']);
    $service = new Google_Service_Analytics($client);    
    // request user accounts
    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
   foreach ($accounts->getItems() as $item) {
    echo "Account: ",$item['name'], "  " , $item['id'], "<br /> n";        
    foreach($item->getWebProperties() as $wp) {
        //echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> n";    
        $views = $wp->getProfiles();
        if (!is_null($views)) {
            foreach($wp->getProfiles() as $view) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> n";    
            }
        }
    }
} // closes account summaries
}
 print_r($_SESSION['token']); 
?>

我 http://www.daimto.com/google-oauth2-php/尝试关注这篇文章。还有一个额外的问题,检索用户分析数据的下一步是什么?希望外面有人不要像我一样困惑,并且可以提供帮助。我也使用 Laravel 5.2 作为框架

只需使用 https://github.com/OzanKurt/google-analytics/一切都会好起来的。 ;)

相关内容

  • 没有找到相关文章

最新更新