Search code examples
oauth-2.0

YouTube Api OATH access token


I want to make a bot that will upload the videos from hard disk to YouTube. I have written the code already but there is a problem. I have to make the authorization manually by logging in to the Google account which gives me an access token, valid for only 1 hour. Now the trouble is the bot wont be able to login to the account and I am unable to use the access token after an hour. I heard about the refresh token but not sure how it works, can you help me with this? The perfect scenario would be having the access token through "Client_id" and "Secret_Key" (I should not have to login)

$client = new Google_Client();
$client->setClientId(YOUTUBE_CLIENT_ID);
$client->setClientSecret(YOUTUBE_CLIENT_SECRET);
$client->setAccessToken(???);

Solution

  • So finally got a hint from ChatGPT :) I had to add these two lines. Didn't know the "setPromt" function existed!

    $client->setPrompt('consent');
    $client->setAccessType('offline');