2016-05-18 53 views
2

我試圖讓這個例子工作: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」文件夾中。

這裏是我的錯誤的截圖去我的網站時:

redirect_uri_mismatch

奇怪的是,如果我定位到上面的鏈接中包含的內容錯誤信息「訪問..... 。更新授權..「我得到這個錯誤信息:」 OAuth客戶端不存在「

The OAuth Client Does Not Exist

如果我點擊我的唯一可用的客戶端ID,我可以導航到看到URI的WHI CH我下面的截圖,以及:

API Screen

正如你所看到的,在授權的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)); 
} 

所有這些代碼是從第一個網站示例獲取的,除了一些小的補充,以使其與我的系統相匹配。

任何人都知道我可以擺脫這個錯誤?我究竟做錯了什麼?

回答

4

請記住,就Google而言,「您的」服務器是敵對的,除非您將其命名爲「友好」,您必須明確列出每一個可能的谷歌OAuth呼叫源。

谷歌是一個clubbouncer,一個大的,醜陋的,不可移動的保鏢與AA客人名單說給你的應用程序:「我將只與您的要求處理,如果你的確切名稱或ID在該列表上」

有你嘗試包括,不僅localhost,但所有其他可能的起源?

您必須列出url「root」的所有可能變體,包括顯式IP。

http://www.example.com 
http://example.com 
https://example.com 
https://www.example.com 
http://222.111.0.111 
... 

不要忘記包括

https://accounts.google.com:443

+1

原點應該是什麼?例如,如果我的項目的URL是www.ffy.thisismydomain.com,那麼源應該仍然是http:// localhost? – NoReceipt4Panda

+1

你說過不要忘記包含這個account.google網址,你的意思是其中的一個URI嗎? – NoReceipt4Panda

+1

等待所有這些URL的不同格式,我應該把它作爲原點還是重定向? – NoReceipt4Panda

2

請求中的重定向Uri必須與您存儲的一個Uri完全相同。

我在您的請求中看到存儲的一個末尾處有一個/。

+0

您是正確的,您列出的各種URL中的至少一個必須與發起請求的網站的URL完全匹配。 –

+0

所以作爲URI,我應該從最後去掉「/」? – NoReceipt4Panda

+1

就是這樣。這種拖尾/在你的情況下似乎毫無用處。 –

3

只是複製URI上的錯誤從錯誤屏幕出現請求,並將其粘貼到OAuth憑證「授權重定向的URI」

現在運行應用程序。 這對我有用。希望我回答你的問題。

+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16645221) – Alberto

+1

我的回答不正確? –

相關問題