php-google-spreadsheet-client
php-google-spreadsheet-client copied to clipboard
Access token is invalid for service-account
require_once __DIR__ . '/../vendor/autoload.php';
use Google\Spreadsheet\ServiceRequestFactory;
use Google\Spreadsheet\DefaultServiceRequest;
function getGoogleTokenFromKeyFile() {
$client = new Google_Client();
$client->setAuthConfig( __DIR__ . '/../config/client_secret.json' );
$client->setScopes(['https://www.googleapis.com/auth/spreadsheets']);
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
return $client->getAccessToken();
}
$tokenArray = getGoogleTokenFromKeyFile();
$serviceRequest = new DefaultServiceRequest($tokenArray['access_token']);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
$spreadsheet = $spreadsheetFeed->getByTitle('Title');
This code give me Access token is invalid My service-account works propery because I tested it using google/apiclient ver 2.0.0
'https://www.googleapis.com/auth/spreadsheets exchange for https://spreadsheets.google.com/feeds
@Brunoalcau are you sure? It is ok for API v4?
@madebyrogal my apologies for the delay, and really only for v3
Hi @madebyrogal,
This library is only for v3. I didn't even know v4 was out until now. I've noticed that they've switched to json from xml which means it will have to be a complete rewrite.
I'm going to start it soon but it will be a while before it's ready to use in production. In he meantime you can continue to use v3, it will be around for some time yet.
Thx @asimlqt :)
Side question: Same problem, but how can we use v3? If I install google API client, there is nothing about a v3 or a v4.
The new API isn't terrible... https://developers.google.com/sheets/guides/batchupdate
Any update on the play here? I'm using v3. My auth works fine for google drive client. I get auth via
$auth = remoteRequest("https://www.googleapis.com/oauth2/v3/token", http_build_query([
"refresh_token" => $refresh_token,
"client_id" => $client_id,
"client_secret" => $secret,
"grant_type" => "refresh_token"
]), false, false, "POST");
But still getting unauthorized request. Anyone found a way to get this working?
I've updated to their new APIs, so this lib is no longer required. Note to anyone else who used the beta api before now, quite a few things have changed. Method names and such.