Beanonymous
Beanonymous

Reputation: 95

Youtube API v3 without the need of End-User Authentication

Im trying to create a Youtube upload script that does not require the end user to login in order to upload a video to my channel. The issue is I can gain the access token but it expires after an hour of being inactive. Also I am unsure where you can get the "refresh_token" from.

So essentially I wish to have this Google API v3 script not require any client side authentication in order to upload video's.

From what I gather from the API documentation at google if I had the "refresh_token" I could renew the token without the need of user interaction required.

Please see below my existing code on the matter.

<?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>

If someone can look over this code and tell me what I'm missing would be great.

Upvotes: 3

Views: 4431

Answers (1)

Beanonymous
Beanonymous

Reputation: 95

I finally figured it out!!!

Thanks to Burcu Dogan for the solution:

Automatically refresh token using google drive api with php script

It turns out that the refresh_token will only be returned on the first $client->authenticate(), for that account you login with and you need to permanently store it remove the need for the user authentication.

As I had already authenticated desired user I was unable to regenerate the refresh_token with this project so I had to delete the project and recreate it.

Re-Authenticate the user and the refresh_token appeared! I then stored this token safe and have not had any issues since.

If you do not wish to delete your project and start again you can also go to oAuth Playground and aquire your refresh_token that way.

YouTube run through

Hope this help solve a lot of peoples problems online.

Upvotes: 2

Related Questions