php-google-spreadsheet-client icon indicating copy to clipboard operation
php-google-spreadsheet-client copied to clipboard

Access token is invalid for service-account

Open madebyrogal opened this issue 8 years ago • 10 comments

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



madebyrogal avatar Jun 22 '16 08:06 madebyrogal

This code give me Access token is invalid My service-account works propery because I tested it using google/apiclient ver 2.0.0

madebyrogal avatar Jun 22 '16 08:06 madebyrogal

'https://www.googleapis.com/auth/spreadsheets exchange for https://spreadsheets.google.com/feeds

Brunoalcau avatar Jul 01 '16 10:07 Brunoalcau

@Brunoalcau are you sure? It is ok for API v4?

madebyrogal avatar Jul 01 '16 10:07 madebyrogal

@madebyrogal my apologies for the delay, and really only for v3

Brunoalcau avatar Jul 04 '16 18:07 Brunoalcau

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.

asimlqt avatar Jul 05 '16 22:07 asimlqt

Thx @asimlqt :)

madebyrogal avatar Jul 06 '16 06:07 madebyrogal

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.

rap2hpoutre avatar Jul 07 '16 09:07 rap2hpoutre

The new API isn't terrible... https://developers.google.com/sheets/guides/batchupdate

codercotton avatar Jul 27 '16 15:07 codercotton

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?

optikalefx avatar Aug 30 '16 13:08 optikalefx

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.

optikalefx avatar Aug 30 '16 14:08 optikalefx