我希望用户通过我的网站在我的YT频道上发送他们的视频。 它在去年有效,但现在Youtube要求用户在Youtube上进行身份验证。
如果现在有可能吗?
以下是代码:
public function getPostParams()
{
$header = array(
"alg" => "RS256",
"typ" => "JWT"
);
$jwt = array(
"iss" => "gulliapp@appspot.gserviceaccount.com",
"scope" => "https://www.googleapis.com/auth/youtube",
"aud" => "https://accounts.google.com/o/oauth2/token",
"exp" => time() + 3600,
"iat" => time()
);
$jwt_string = JWT::encode($jwt, null, 'RS256', "52e47342c1d175e88a94e0076e7a87474013a798", $header);
$headers = array(
"Content-type: application/x-www-form-urlencoded",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 3000);
curl_setopt($ch, CURLOPT_POST, true);
$post = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=" . $jwt_string ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$res = curl_exec($ch);
$url = curl_getinfo($ch);
$token = json_decode($res);
curl_close($ch);
return $token->access_token;
}
public function saveFile($file, $name, $description, $token)
{
$client = new Google_Client();
$client->setClientId(self::$OAUTH2_CLIENT_ID);
$client->setClientSecret(self::$OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://gulli.ru/videocontest/add/', FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$client->setAccessToken($token);
$client->setAccessType("offline");
$youtube = new Google_Service_YouTube($client);
if ($client->getAccessToken()) {
try{
$videoPath = $file['tmp_name'];
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle("web2016");
$snippet->setDescription("web2016");
$snippet->setTags(array("tag1", "tag2"));
$snippet->setCategoryId("22");
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "private";
// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$insertRequest = $youtube->videos->insert("status,snippet", $video);
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($videoPath));
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$client->setDefer(false);
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
$_SESSION['token'] = $client->getAccessToken();
}
return false;
}