我試圖讓這個例子工作:https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php#enableGoogle Analytics(分析)OAuth2:如何解決錯誤:「redirect_uri_mismatch」?
我得到的錯誤是「錯誤:redirect_uri_mismatch」。
爲了安裝谷歌的API資源,我用作曲家用這個命令:
php composer.phar require google/apiclient:^[email protected]
這在我的根站點文件夾中安裝了「供應商」文件夾中。我的index.php和oauth2callback.php文件位於「public_html」文件夾中。
這裏是我的錯誤的截圖去我的網站時:
奇怪的是,如果我定位到上面的鏈接中包含的內容錯誤信息「訪問..... 。更新授權..「我得到這個錯誤信息:」 OAuth客戶端不存在「
如果我點擊我的唯一可用的客戶端ID,我可以導航到看到URI的WHI CH我下面的截圖,以及:
正如你所看到的,在授權的JavaScript來源,我已經http://localhost上市,並在授權的重定向URI的,我有我的生活網站其次是「oauthc2callback。 php「文件擴展名。
我不明白如何擺脫我得到的錯誤。我試過替換URI並放入不同的JavaScript起源。
此外,出於某種原因,在上一次截圖中,它表示我沒有權限編輯此OAuth客戶端,但我可以進行編輯。
我對index.php文件的代碼:
<?php
// Load the Google API PHP Client Library.
require_once '../vendor/autoload.php';
// Start a session to persist credentials.
session_start();
// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();
$client->setAuthConfigFile('../config/client_secrets.json');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Create an authorized analytics service object.
$analytics = new Google_Service_Analytics($client);
// Get the first view (profile) id for the authorized user.
$profile = getFirstProfileId($analytics);
// Get the results from the Core Reporting API and print the results.
$results = getResults($analytics, $profile);
printResults($results);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
function getFirstprofileId(&$analytics) {
// Get the user's first view (profile) ID.
// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();
// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);
if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$firstPropertyId = $items[0]->getId();
// Get the list of views (profiles) for the authorized user.
$profiles = $analytics->management_profiles
->listManagementProfiles($firstAccountId, $firstPropertyId);
if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();
// Return the first view (profile) ID.
return $items[0]->getId();
} else {
throw new Exception('No views (profiles) found for this user.');
}
} else {
throw new Exception('No properties found for this user.');
}
} else {
throw new Exception('No accounts found for this user.');
}
}
function getResults(&$analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions');
}
function printResults(&$results) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) > 0) {
// Get the profile name.
$profileName = $results->getProfileInfo()->getProfileName();
// Get the entry for the first entry in the first row.
$rows = $results->getRows();
$sessions = $rows[0][0];
// Print the results.
print "<p>First view (profile) found: $profileName</p>";
print "<p>Total sessions: $sessions</p>";
} else {
print "<p>No results found.</p>";
}
}
我有 「oauth2callback.php」 代碼:
<?php
require_once '../vendor/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('../config/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/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'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
所有這些代碼是從第一個網站示例獲取的,除了一些小的補充,以使其與我的系統相匹配。
任何人都知道我可以擺脫這個錯誤?我究竟做錯了什麼?
原點應該是什麼?例如,如果我的項目的URL是www.ffy.thisismydomain.com,那麼源應該仍然是http:// localhost? – NoReceipt4Panda
你說過不要忘記包含這個account.google網址,你的意思是其中的一個URI嗎? – NoReceipt4Panda
等待所有這些URL的不同格式,我應該把它作爲原點還是重定向? – NoReceipt4Panda