user3415627
user3415627

Reputation: 21

PHP Youtube API v3 Oauth access token errror

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.

Upvotes: 2

Views: 3168

Answers (1)

Vinicius Braz Pinto
Vinicius Braz Pinto

Reputation: 8289

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);
    }
}

Upvotes: 2

Related Questions