【发布时间】: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