谷歌API V3 redirect_uri_mismatch错误

问题描述:

我是试图让谷歌帐户认证为我的应用程序,但是当我选择的帐号登录我的错误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> 

我找到了解决这个

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

替换该行

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

后,我不知道为什么它不工作时,它需要重定向类的全路径

您选择的凭据类型取决于您要构建的应用程序。 'Web应用程序的客户端ID'应该可以正常工作。

您在Redirect URIs中指定的URI必须指向实际的脚本文件,如http://example.com/index.php。我不认为http://example.com应该工作。

+0

我已经尝试过这个解决方案但是不行 – developer 2014-10-11 05:27:29