I am creating a small php script to add some videos to a YouTube playlist, the part of getting an access token and adding the videos is working without an issue, but once the access token expires I am unable to get a new one using the refresh token, the fetchAccessTokenWithRefreshToken() always returns an exception 'Invalid token format'
The original acccess token is this one:
{"access_token":"ya29.a0Ad52N38HS-GI6Sh0sdfsdfB70aU815np3b9PlnD4n7BOxrjXnJpf_rxVPWsdfdsf3UMW3bWuUHfmKn9BDxz6ob592LiByk2jeUYOdnRVKb6rC1CKDkKQMy1fSbq8sdfsdfNYBJDsEeKZEHlHgaWsfsdfQVEf4FY2oQaCgYKAdMSARESFQHGX2MigIIWK5RkOe8krHadR_K3hQ0173","expires_in":3599,"scope":"https:\/\/www.googleapis.com\/auth\/youtube","token_type":"Bearer","created":1712552401,"refresh_token":"1\/\/09HLLzFdZusdfsdfAAGAkSNwF-L9IrWLZCMaBh9G-uedsdfdsfkl6SG8S9OEpvm_2r3USiaOsdfdfuZWruUXSb9YA3YA"}
And I am saving it inside:
$accessTokenFilePath = "Library/accessToken.txt";
Then the code goes as follows:
$client = new Google_Client();$client->setApplicationName('YouTube Parser');$client->setScopes(Google_Service_YouTube::YOUTUBE);$client->setClientId('client_id_goes_here');$client->setClientSecret('client_secret_goes_here');$client->setAccessType('offline');$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');$accessToken = json_decode(file_get_contents($accessTokenFilePath), true);$client->setAccessToken($accessToken);if ($client->isAccessTokenExpired()) { try { $accessToken = $client->fetchAccessTokenWithRefreshToken($accessToken['refresh_token']); $client->setAccessToken($accessToken); } catch (Exception $e) { // It always gives an exception invalid token format echo " Caught Exception: " . $e->getMessage();echo "\r\n"; }}
I tried saving the refresh token in a variable then passing this variable to the fetchAccessTokenWithRefreshToken() method, I also tried to save the refresh token in its own file and read it from the file, but nothing is working !