需要制作用户授权,以便服务器将视频上传到用户频道。
身份验证部分:
$client = new Google_Client();
$client->setAuthConfigFile(PROPPATH.'/inc/client_secret.json');
$client->setRedirectUri('https://back url');
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessType('offline');
$credentialsPath = PROPPATH.'/youtube_auth/user_'.get_current_user_id().'.json';
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
$authUrl = $client->createAuthUrl();
if(!isset($_GET['code'])) {
echo '<script>window.location = "'.$authUrl.'";</script>';
} else {
$authCode = $_GET['code'];
$accessToken = $client->authenticate($authCode);
file_put_contents($credentialsPath, $accessToken);
}
身份验证似乎有效,并保存了一些密钥。 第二部分,尝试上传视频:
$client = new Google_Client();
$client->setAuthConfigFile(PROPPATH.'/inc/client_secret.json');
$client->setScopes('https://www.googleapis.com/auth/youtube');
$youtube = new Google_Service_YouTube($client);
$client->setAccessToken(file_get_contents(PROPPATH.'/youtube_auth/user_2.json'));
$client->setAccessType('offline');
if ($client->getAccessToken()) {
$video = new Google_Service_YouTube_Video();
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$insertRequest = $youtube->videos->insert("", $video);
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($videoPath));
...
我收到错误:
发生了服务错误:{&#34;错误&#34;:{&#34;错误&#34;:[{&#34;域&#34;: &#34;全球&#34;,&#34;原因&#34;:&#34; authError&#34;,&#34;消息&#34;:&#34;无效的凭证&#34;, &#34; locationType&#34;:&#34;标题&#34;,&#34;位置&#34;:&#34;授权&#34; }],&#34;代码&#34;: 401,&#34;消息&#34;:&#34;无效的凭证&#34; }} 我错过了什么?
答案 0 :(得分:0)
401:凭据无效
无效的授权标头。您正在使用的访问令牌也是 已过期或无效。
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization",
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
建议的操作:使用长期存取来刷新访问令牌 刷新令牌。