我是Google API的新手,並且無法使Google Analytics API正常工作。到目前爲止,我已經設置了開發者控制檯,創建了一個項目,並生成了相關的證書。我註冊的電子郵件ID是[email protected]
,默認情況下重定向URI設置爲http://www.mycompany.com/oauth2callback
。 JavaScript起源設置爲http://www.mycompany.com
。Google API重定向URI無法正常工作
當我從localhost運行項目時,我可以啓動OAuth過程。但是當我點擊「允許」按鈕時,我被髮送到http://www.mycompany.com
並沒有任何反應。我可能做錯了什麼?我是否需要從mycompany.com doamin運行此腳本才能正常工作?最後,我如何從本地主機運行它?
<?php
include_once "google_api/autoload.php";
session_start();
$client = new Google_Client();
$client->setApplicationName("Hello Analytics API Example");
$client->setClientId('XXXXXXXXXXXXXXXXX');
$client->setClientSecret('XXXXXXXXXXXXXXXXXXXX');
//$client->setRedirectUri('XXXXXXXXXXXXXXXXXXX');
$client->setRedirectUri('XXXXXXXXXXXXXXXXXX');
$client->setDeveloperKey('XXXXXXXXXXXXXXXXXXXX');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
// Magic. Returns objects from the Analytics Service instead of associative arrays.
//print_r($client);
//$client->setUseObjects(true);
if (isset($_GET['code']))
{
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (!$client->getAccessToken())
{
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
else
{
// Create analytics service object. See next step below.
$analytics = new apiAnalyticsService($client);
runMainDemo($analytics);
}
function runMainDemo(&$analytics) {
try {
// Step 2. Get the user's first view (profile) ID.
$profileId = getFirstProfileId($analytics);
if (isset($profileId)) {
// Step 3. Query the Core Reporting API.
$results = getResults($analytics, $profileId);
// Step 4. Output the results.
printResults($results);
}
} catch (apiServiceException $e) {
// Error from the API.
print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
print 'There wan a general error : ' . $e->getMessage();
}
}
function getFirstprofileId(&$analytics) {
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();
$webproperties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);
if (count($webproperties->getItems()) > 0) {
$items = $webproperties->getItems();
$firstWebpropertyId = $items[0]->getId();
$profiles = $analytics->management_profiles
->listManagementProfiles($firstAccountId, $firstWebpropertyId);
if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();
return $items[0]->getId();
} else {
throw new Exception('No views (profiles) found for this user.');
}
} else {
throw new Exception('No webproperties found for this user.');
}
} else {
throw new Exception('No accounts found for this user.');
}
}
function getResults(&$analytics, $profileId) {
return $analytics->data_ga->get(
'ga:' . $profileId,
'2012-03-03',
'2012-03-03',
'ga:sessions');
}
function printResults(&$results) {
if (count($results->getRows()) > 0) {
$profileName = $results->getProfileInfo()->getProfileName();
$rows = $results->getRows();
$sessions = $rows[0][0];
print "<p>First view (profile) found: $profileName</p>";
print "<p>Total sessions: $sessions</p>";
} else {
print '<p>No results found.</p>';
}
}
你想讓別人能看到Google Analytics(分析)數據,或者您想讓其他人訪問您的Google Analytics(分析)數據嗎?在我們可以幫助您之前,您確實需要添加一些代碼。 – DaImTo 2014-11-06 08:45:03
@DalmTo我想創建一個請求用戶權限並從其Analytics(分析)中提取只讀數據的應用程序。現在我正在通過自己的帳戶進行測試,因此開發人員和用戶帳戶都是'myname @ mycompany.com'。代碼補充,雖然它只是官方教程。 – dotslash 2014-11-06 08:48:24