尝试通过快速入门指南,通过PHP(https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-php)使Google Analytics API正常运行。当页面加载并跳转到Google进行授权时,回调页面在从Google返回时不会加载:oauth2callback.php?code=4/xvilrDCNpY6MpFd6cqe2OoWNINiodiZCC0iyX8ZtNtM#
我只是从Chrome获得以下内容,没有任何PHP错误或其他任何内容:
未收到任何数据 ERR_EMPTY_RESPONSE
oauth2callback.php:
<?php
require_once 'google-api-php-client/src/Google/autoload.php';
// Start a session to persist credentials.
session_start();
// Create the client object and set the authorization configuration
// from the client_secrets.json you downloaded from the Developers Console.
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/jeremy/oauth2callback.php');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/jeremy/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}