gpt4 book ai didi

php - 无需最终用户身份验证的 Youtube API v3

转载 作者:行者123 更新时间:2023-12-05 05:26:05 25 4
gpt4 key购买 nike

我正在尝试创建一个不需要最终用户登录即可将视频上传到我的 channel 的 Youtube 上传脚本。问题是我可以获得访问 token ,但它会在闲置一小时后过期。此外,我不确定您可以从哪里获得“refresh_token”。

所以基本上我希望这个 Google API v3 脚本不需要任何客户端身份验证即可上传视频。

根据我从 Google 的 API 文档中收集到的信息,如果我有“refresh_token”,我可以在不需要用户交互的情况下更新 token 。

请在下面查看我现有的关于此事的代码。

<?php

$key = json_decode(file_get_contents('the_key.txt'));

require_once 'Client.php';
require_once 'Service/YouTube.php';
session_start();

$OAUTH2_CLIENT_ID = 'YOUR_CLIENT_ID.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'YOUR_SECRET_KEY';
$REDIRECT = 'http://example.com/test.php';//Must be exact as setup on google API console
$APPNAME = "Test upload app";

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setRedirectUri($REDIRECT);
$client->setApplicationName($APPNAME);
$client->setAccessType('offline');

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) {
//if session current save to file updates token key
$client->setAccessToken($_SESSION['token']);
echo '<code>' . $_SESSION['token'] . '</code><br>';
file_put_contents('the_key.txt', $_SESSION['token']);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
if($client->isAccessTokenExpired()) {
echo "Token Expired<br>Attempt Refresh: ";
$client->refreshToken($key->access_token);
//refresh token, now need to update it in session
$_SESSION['access_token']= $client->getAccessToken();
}

$_SESSION['token'] = $client->getAccessToken();

///////////////////////////////////////////////////////////////////////
//Run Upload script here from google Youtube API v3
//https://developers.google.com/youtube/v3/code_samples/php#resumable_uploads
///////////////////////////////////////////////////////////////////////

} else {
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
$htmlBody = '<h3>Authorization Required</h3><p>You need to <a href="$authUrl">authorise access</a> before proceeding.<p>';
}

?>



<!doctype html>
<html>
<head>
<title>My Uploads</title>
</head>
<body>
<?php echo $htmlBody?>
</body>
</html>

如果有人可以查看这段代码并告诉我我遗漏了什么,那就太好了。

最佳答案

我终于明白了!!!

感谢Burcu Dogan解决方案:

Automatically refresh token using google drive api with php script

事实证明,refresh_token 只会在第一个 $client->authenticate() 时返回,对于您登录的那个帐户,您需要永久存储它,从而消除了对用户身份验证的需要。

因为我已经对所需用户进行了身份验证,所以我无法使用该项目重新生成 refresh_token,因此我不得不删除该项目并重新创建它。

重新认证用户,refresh_token出现了!然后我安全地存储了这个 token ,此后没有任何问题。

如果您不想删除您的项目并重新开始,您也可以转到 oAuth Playground并以这种方式获取您的 refresh_token。

YouTube run through

希望这有助于解决很多人的在线问题。

关于php - 无需最终用户身份验证的 Youtube API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28340578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com