2
votes

I use this script: https://developers.google.com/youtube/v3/code_samples/php#resumable_uploads to upload videos to youtube. Everything works fine, but next day I have error:

An client error occurred: The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.

I looked at some tips on how to fix this but I did not succeed. I am new to programming. Please help me fix this error.

1

1 Answers

2
votes

You need to set the access type to offline which allows you to refresh the access token so you can authenticate the app without the user having to give authorization again.

Check the docs.

I can't test this now, but try the following. This isn't ideal though, you should be persisting the token somewhere.

// After "$client->setRedirectUri($redirect);" add:
$client->setAccessType('offline');


// After "$client->setAccessToken($_SESSION['token']);" add:
if ($client->isAccessTokenExpired()) {
    $currentTokenData = json_decode($_SESSION['token']);
    if (isset($currentTokenData->refresh_token)) {
        $client->refreshToken($currentTokenData->refresh_token);
    }
}