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

Youtube API v3 without the need of End-User Authentication

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

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

根据我从 google 的 API 文档中收集到的信息,如果我有 "refresh_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!然后我安全地存储了这个令牌,此后没有任何问题。

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

YouTube run through

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