为什么不是';我的谷歌OAuth 2.0正在运行


Why isn't my Google OAuth 2.0 Working?

我一直在编写一个小脚本来获取YouTube频道数据,但我的Google OAuth 2.0不起作用。

$validate = "https://accounts.google.com/o/oauth2/auth?client_id=242340718758-65veqhhdjfl21qc2klkfhbcb19rre8li.apps.googleusercontent.com&redirect_uri=http://conor1998.web44.net/php/oauth.php&scope=https://www.googleapis.com/auth/yt-analytics.readonly&response_type=code&access_type=offline";
echo "<a href='$validate'>Login with Google for advanced analytics</a>";
if(isset($_GET['code'])) {
    // try to get an access token
    $code = $_GET['code'];
    $url = 'https://accounts.google.com/o/oauth2/token?code='.$code.'&client_id=242340718758-65veqhhdjfl21qc2klkfhbcb19rre8li.apps.googleusercontent.com&client_secret={secret}&redirect_uri=http://conor1998.web44.net/php/oauth.php&grant_type=authorization_code';
    $url = urlencode($url);
    header('Location: $url');
}

$response = file_get_contents($url);
$response = json_decode($response);

$channel_data = file_get_contents('https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel==mine&start-date=2014-08-01&end-date=2014-09-01&metrics=views&key=AIzaSyDTxvTLWXStUrhzgCDptVUG4dGBCpyL9MY?alt=json');
$channel_data = json_decode($channel_data, true);
echo "<br />";
var_dump($channel_data);
echo "<br />";

我不知道为什么它不起作用。我觉得这主要是因为我的目标是为用户获取身份验证令牌,这样我就可以获取他们的YouTube数据。如有任何帮助,将不胜感激

您从auth网站收到的代码不是访问令牌!您必须用它来交换刷新和访问令牌(请参阅#4)。

您必须执行POST请求https://accounts.google.com/o/oauth2/token以便获得您的代币。它无法通过GET工作(单击链接时可以看到)。