【问题标题】:Google YouTube API -- 401 on a delete requestGoogle YouTube API -- 删除请求时出现 401
【发布时间】:2019-12-27 21:52:27
【问题描述】:

我正在尝试通过 YouTube API 删除视频并获得 401 状态代码,告诉我需要登录:

YouTube delete exception: Client error: `DELETE https://www.googleapis.com/youtube/v3/videos?id=qHwE11JqiW8&onBehalfOfContentOwner=SNIP&prettyPrint=1` resulted in a `401 Unauthorized` response: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", (truncated...)

这是创建我的 Google 客户端对象的代码:

    $google_client = new Google_Client();
    $google_client->setHttpClient($this->httpClient);
    $google_client->addScope([\Google_Service_YouTube::YOUTUBE, \Google_Service_YouTubePartner::YOUTUBEPARTNER]);
    // This ensures the token is refreshed automatically for the life of the
    // client.
    $google_client->setAccessType('offline');
    if (!empty($google_auth_config['client_id_json'])) {
      $decoder = new JsonDecode(TRUE);
      $google_client->setAuthConfig($decoder->decode($google_auth_config['client_id_json'], 'json'));
    }
    if (!empty($google_auth_config['refresh_token'])) {
      $google_client->fetchAccessTokenWithRefreshToken($google_auth_config['refresh_token']);

    }
    if (!empty($google_auth_config['redirect_uri'])) {
      $google_client->setRedirectUri($google_auth_config['redirect_uri']);
    }

    return $google_client;

var_dump($google_client) 有这个,它看起来确实有它需要的身份验证部分(access_token 和 refresh_token)。 ["token":"Google_Client":private]=> array(6) { ["access_token"]=> string(131) "a big long hash looking thing generated by Google Client fetchAccessTokenWithRefreshToken()" ["expires_in"]=> int(3600) ["scope"]=> string(86) "https://www.googleapis.com/auth/youtubepartner https://www.googleapis.com/auth/youtube" ["token_type"]=> string(6) "Bearer" ["created"]=> int(1566992903) ["refresh_token"]=> string(45) "token generated by Google Oauth2" }

【问题讨论】:

  • PS -- $queryParams 确实包含正确的onBehalfOfContentOwner 值。
  • 您应该阅读完整的回复,而不仅仅是状态码,它会让您了解正在发生的事情。
  • 我添加了另一个语句来打印消息正文。没有一个,所以也没有帮助。
  • id10t 错误在这里。所有这些代码为响应所做的只是创建一个新的,它没有得到服务调用的结果。

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


【解决方案1】:

如果您查看该方法的文档,它会告诉您响应的含义

video.delete

【讨论】:

  • 对,如果成功,我应该得到 204。我得到 200,想知道为什么。文档有关于 4xx 的各种帮助,但没有关于 2xx 的帮助。
  • 我的 exprece 200 和 204 中的 TBH 在 google apis 中是一样的,它可能只是缺少文档通知。在 $response 上转储的完整响应是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2022-01-10
  • 2014-12-02
  • 2017-11-22
  • 2019-08-07
  • 2018-05-28
相关资源
最近更新 更多