2014-10-10 129 views
0

我是試圖讓谷歌帳戶認證爲我的應用程序,但是當我選擇的帳號登錄我的錯誤redirect_uri_mismatch, 在谷歌控制檯我創建Web應用程序 第一客戶端ID我嘗試使用以下設置谷歌API V3 redirect_uri_mismatch錯誤

REDIRECT URIS : http://localhost/myapppath/ 

運行本地主機上的應用程序,但得到了同樣的錯誤 也是我嘗試在Heroku的主辦我的應用程序與Web應用程序下面客戶端ID設置 enter image description here

,但得到了同樣的錯誤,我搜索的計算器多次沒有成功找到一個解決方案

這是我的代碼,它是一個測試代碼來獲得用戶的上傳的視頻

require_once 'Google/Client.php'; 
require_once 'Google/Service/YouTube.php'; 
session_start(); 

/* 
* You can acquire an OAuth 2.0 client ID and client secret from the 
* Google Developers Console <https://console.developers.google.com/> 
* For more information about using OAuth 2.0 to access Google APIs, please see: 
* <https://developers.google.com/youtube/v3/guides/authentication> 
* Please ensure that you have enabled the YouTube Data API for your project. 
*/ 
$OAUTH2_CLIENT_ID = '42965713xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxercontent.com'; 
$OAUTH2_CLIENT_SECRET = 'y9AWlbxxxxxxxDqdQwJ'; 

$client = new Google_Client(); 
$client->setClientId($OAUTH2_CLIENT_ID); 
$client->setClientSecret($OAUTH2_CLIENT_SECRET); 
$client->setScopes('https://www.googleapis.com/auth/youtube'); 
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 
    FILTER_SANITIZE_URL); 
$client->setRedirectUri($redirect); 

// Define an object that will be used to make all API requests. 
$youtube = new Google_Service_YouTube($client); 

if (isset($_GET['code'])) { 
    if (strval($_SESSION['state']) !== strval($_GET['state'])) { 
    die('The session state did not match.'); 
    } 

    $client->authenticate($_GET['code']); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: ' . $redirect); 
} 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

// Check to ensure that the access token was successfully acquired. 
if ($client->getAccessToken()) { 
    try { 
    // Call the channels.list method to retrieve information about the 
    // currently authenticated user's channel. 
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
     'mine' => 'true', 
    )); 

    $htmlBody = ''; 
    foreach ($channelsResponse['items'] as $channel) { 
     // Extract the unique playlist ID that identifies the list of videos 
     // uploaded to the channel, and then call the playlistItems.list method 
     // to retrieve that list. 
     $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads']; 

     $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
     'playlistId' => $uploadsListId, 
     'maxResults' => 50 
    )); 

     $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>"; 
     foreach ($playlistItemsResponse['items'] as $playlistItem) { 
     $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'], 
      $playlistItem['snippet']['resourceId']['videoId']); 
     } 
     $htmlBody .= '</ul>'; 
    } 
    } catch (Google_ServiceException $e) { 
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', 
     htmlspecialchars($e->getMessage())); 
    } catch (Google_Exception $e) { 
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', 
     htmlspecialchars($e->getMessage())); 
    } 

    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $state = mt_rand(); 
    $client->setState($state); 
    $_SESSION['state'] = $state; 

    $authUrl = $client->createAuthUrl(); 
    $htmlBody = <<<END 
    <h3>Authorization Required</h3> 
    <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> 
END; 
} 
?> 

<!doctype html> 
<html> 
    <head> 
    <title>My Uploads</title> 
    </head> 
    <body> 
    <?=$htmlBody?> 
    </body> 
</html> 

回答

0

我找到了解決這個

$redirect = 'http://localhost/myappname'; 

替換該行

$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL); 

後,我不知道爲什麼它不工作時,它需要重定向類的全路徑

2

您選擇的憑據類型取決於您要構建的應用程序。 'Web應用程序的客戶端ID'應該可以正常工作。

您在Redirect URIs中指定的URI必須指向實際的腳本文件,如http://example.com/index.php。我不認爲http://example.com應該工作。

+0

我已經嘗試過這個解決方案但是不行 – developer 2014-10-11 05:27:29