0

我正在使用谷歌分析 php api 来检索我的数据。在 index.php 上,我完成了授权过程,工作正常,我在此页面上成功登录我有指向另一个 php 页面的链接,我想在其中显示我的所有属性,以便我如何将我的登录凭据发布到 properties.php 并创建一个分析对象以检索所有 Web 属性

代码 index.php 看起来像这样

session_start();
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';



$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('XXXX');
$client->setClientId('XXXXXX');
$client->setClientSecret('XXXXX');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('XXXXXX'); // API key
$client->setScopes('https://www.googleapis.com/auth/analytics');


$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
    die('Logged out.');
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}
TRY{

    }

?>
4

0 回答 0