asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

Access token is invalid for service-account #143

Open madebyrogal opened 8 years ago

madebyrogal commented 8 years ago
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 commented 8 years ago

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

Brunoalcau commented 8 years ago

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

madebyrogal commented 8 years ago

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

Brunoalcau commented 8 years ago

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

asimlqt commented 8 years ago

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.

madebyrogal commented 8 years ago

Thx @asimlqt :)

rap2hpoutre commented 8 years ago

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.

codercotton commented 8 years ago

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

optikalefx commented 8 years ago

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 commented 8 years ago

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.