Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Uncaught exception 'Google\Spreadsheet\UnauthorizedException' with message 'Access token is invalid' or Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_grant: i'' #43

Closed
nekromoff opened this issue Dec 14, 2014 · 5 comments

Comments

@nekromoff
Copy link

I have this problem, I always get this:

{"access_token":"ya29.3AAc-8mXbyHSaOFS_TJJX4dkrvH92eY956HV5ZyR1mR6gTnQnr-jPzZkDEa0rJl9pOaR4lJ3DdGJuQ","token_type":"Bearer","expires_in":3600,"created":1418520786}
Fatal error: Uncaught exception 'Google\Spreadsheet\UnauthorizedException' with message 'Access token is invalid' in /whatever/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php:240 Stack trace: #0 /whatever/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php(131): Google\Spreadsheet\DefaultServiceRequest->execute(Resource id #46) #1 /whatever/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/SpreadsheetService.php(38): Google\Spreadsheet\DefaultServiceRequest->get('feeds/spreadshe...') #2 /whatever/sample.php(27): Google\Spreadsheet\SpreadsheetService->getSpreadsheets() #3 {main} thrown in /whatever/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php on line 240

scopes:
$client->setScopes(array('https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive','https://docs.google.com/feeds/'));

spreadsheet access code:
$serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($token['access_token']);
Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();

sometimes i get this instead:
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_grant: i'' in /whatever/vendor/google/apiclient/src/Google/Auth/OAuth2.php:127 Stack trace: #0 /whatever/vendor/google/apiclient/src/Google/Client.php(130): Google_Auth_OAuth2->authenticate('4/gGfFncBJOs5id...') #1 /whatever/sample.php(19): Google_Client->authenticate('4/gGfFncBJOs5id...') #2 {main} thrown in /whatever/vendor/google/apiclient/src/Google/Auth/OAuth2.php on line 127

@asimlqt
Copy link
Owner

asimlqt commented Jan 6, 2015

There seems to be something wrong with the access token. Can you try and generate one using the following code and let me know if you still have an issue.

https://github.com/asimlqt/php-google-oauth

Note: The access token is only valid for one hour.

@Abban
Copy link

Abban commented Apr 7, 2015

@nekromoff I ran into the same problem, the issue is the Google api saves the access token as encoded json. I fixed it with:

$token = json_decode($client->getAccessToken());
$serviceRequest = new DefaultServiceRequest($token->access_token);
ServiceRequestFactory::setInstance($serviceRequest);

@oxidand
Copy link

oxidand commented Jul 31, 2015

I have the same issue, access token generated with official PHP API from Google is not working with this library
I already tried to decode the token, but nothing have changed
Drive API is enabled in my dev console

Fatal error: Uncaught exception 'Google\Spreadsheet\UnauthorizedException' with message 'Access token is invalid' in /site/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php:250

Stack trace:

#0 /site/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php(132): Google\Spreadsheet\DefaultServiceRequest->execute(Resource id #35)

#1 /site/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/SpreadsheetService.php(38): Google\Spreadsheet\DefaultServiceRequest->get('feeds/spreadshe...')

#2 /site/index.php(51): Google\Spreadsheet\SpreadsheetService->getSpreadsheets()

#3 {main}
thrown in /site/vendor/asimlqt/php-google-spreadsheet-client/src/Google/Spreadsheet/DefaultServiceRequest.php on line 250

Access token seems to be valid, because I can access my drive using official Google API,
but I need to change spreadsheets, so I'd like to use this lib

@nekromoff
Copy link
Author

this works for me (using .p12 key):

session_start();

define('GOOGLE_CLIENT_ID','');
define('GOOGLE_CLIENT_EMAIL','');
define('GOOGLE_SPREADSHEETS_SCOPE','https://spreadsheets.google.com/feeds');
define('GOOGLE_APPLICATION_NAME','whatever');
define('GOOGLE_KEY_FILE','xxxxxxxxxxxxxx.p12'); // pass for key: notasecret

function getToken()
{
    $client = new Google_Client();
    $client->setApplicationName(GOOGLE_APPLICATION_NAME);
    $client->setClientId(GOOGLE_CLIENT_ID);

    $key = file_get_contents(GOOGLE_KEY_FILE);
    $cred = new Google_Auth_AssertionCredentials(
        GOOGLE_CLIENT_EMAIL,
        array(GOOGLE_SPREADSHEETS_SCOPE),
        $key
    );

    $client->setAssertionCredentials($cred);

    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }

    $service_token = json_decode($client->getAccessToken());
    return $service_token->access_token;
}

require("vendor/autoload.php");

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

$_SESSION['access_token']=getToken();

$serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($_SESSION['access_token']);
Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
'''

@oxidand
Copy link

oxidand commented Jul 31, 2015

https://spreadsheets.google.com/feeds was the solution. I tried so many different scopes from Google Dev Docs, but this one wasn't listed there. Even with full access lib can't access spreadsheets. So it solved the issue ;)

@asimlqt asimlqt closed this as completed Mar 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants