【问题标题】:Youtube API v3 without the need of End-User Authentication无需最终用户身份验证的 Youtube API v3
【发布时间】:2015-04-05 02:35:56
【问题描述】:

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

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

根据我从谷歌的 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>

如果有人可以查看此代码并告诉我我缺少什么,那就太好了。

【问题讨论】:

    标签: php upload access-token google-api-php-client youtube-data-api


    【解决方案1】:

    我终于明白了!!!

    感谢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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2016-10-08
      • 2013-10-27
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多