【问题标题】:Youtube data api asks for consent while accesstype is set to offline访问类型设置为离线时 Youtube 数据 api 请求同意
【发布时间】:2019-08-15 16:58:15
【问题描述】:

我在一个网站上实现了 Google Youtube data api,让用户可以看到某些频道的视频。我已阅读 Google 提供的有关如何使用它的文档,并使用了离线访问类型,因为它允许 API 自动执行,在访问令牌过期后刷新访问令牌。

API 正在运行,但我仍然会在 1 小时后提示进行自动化(访问令牌到期的默认时间),同时拥有自动刷新访问令牌的代码。

我没有提供我使用的所有代码,只提供处理 API 设置和令牌到期的代码,因为其他代码工作正常。

对我在这里做错的任何帮助将不胜感激。
更新

$client = new Google_Client();
$client->setAuthConfigFile($config);
$client->setRedirectUri($redirect);
$client->addScope('https://www.googleapis.com/auth/youtube.readonly');
$client->setAccessType('offline');

if(file_exits($aToken){
$accessToken = file_get_contents($aToken);
$client->setAccessToken($accessToken);

if($client->isAccessTokenExpired()) {
  if($client->getRefreshToken()){
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    $client->setAccessToken($client->getAccessToken());
    file_put_contents($aToken, $client->getAccessToken());
  } else {
      authorize();
    }
}

【问题讨论】:

    标签: php google-api google-oauth youtube-data-api google-api-php-client


    【解决方案1】:

    您正在应用刷新令牌,但实际上并未使用它来获取新的访问令牌。

    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    $client->setAccessToken($client->getAccessToken()); 
    

    我的样本Oauth2Authentication.php

    【讨论】:

    • 谢谢,我也没有任何刷新令牌,因为我没有将approvalprompt 参数设置为“force”。这样和 fetch 方法解决了这个问题。有趣的是,在 youtube 数据 api 的文档中没有提到 fetch 方法,我唯一能找到的地方是 people api。
    • Google 文档对于所有 api 都不相同,有些比其他的文档更好。驱动器对 php 有好处,所以谷歌分析我的项目已经为所有 api 生成了它们。如果对您有帮助,请记住接受答案。
    • 现在接受并支持您的答案。是的,那个代码示例看起来不错,所以将来可能会在 Google apis 中使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2017-09-24
    • 1970-01-01
    • 2012-02-23
    • 2020-05-30
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多