【问题标题】:Why isn't my Google OAuth 2.0 Working?为什么我的 Google OAuth 2.0 不工作?
【发布时间】:2014-12-24 09:25:07
【问题描述】:

我一直在编写一个小脚本来获取 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 数据。任何帮助将不胜感激

【问题讨论】:

    标签: php oauth youtube-api google-oauth


    【解决方案1】:

    您从认证网站收到的代码不是访问令牌!你必须exchange it for refresh and access tokens(见#4)。

    您必须向https://accounts.google.com/o/oauth2/token 执行 POST 请求才能获取您的令牌。它不能通过 GET 工作(正如您在单击链接时所看到的那样)。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多