Trying to Get Google accessToken

2020-05-03 09:46发布

问题:

It seems like, no matter what I do, Google is trying its very best to stop me from completing this research project. My project has me using a Google Spreadsheet as a database, and use the data from said spreadsheet to perform a programmatic Google Images search, and display a couple of the results to the end-user.

The setup instructions

I have started with following the instructions here: https://github.com/asimlqt/php-google-spreadsheet-client . I downloaded Composer to my computer, and then follow the instructions here and here to have Composer download the necessary libraries to my project.

The coding process From there, I tried to replicate what this guy was doing in his answer. In fact, here is my code:

<?php
    require "vendor/autoload.php";
    //require '/php-google-spreadsheet-client-master\src\Google\Spreadsheet\Autoloader.php';

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

    const GOOGLE_CLIENT_ID = 'someClientID'; // here, I just pulled the default client ID from my Google Developers account
    const GOOGLE_CLIENT_EMAIL = 'someClientEmail'; // I used the default client email here as well, again, straight from Google Developers account
    const GOOGLE_CLIENT_KEY = 'someKey'; // I used the key that I used for the Google Custom Search Engine 
    const GOOGLE_CLIENT_KEY_PATH = 'vendor\google\apiclient\examples\key.p12'; // I did a search on my project folder for the .p12 file that had the key information, and used that path here
    const G_CLIENT_KEY_PW   = 'notasecret'; // the default (I don't know why I need to do this)

    // setup the googleClient
    $googleClient = new Google_Client();
    $googleClient->setApplicationName('future-graph-611');
    $googleClient->setClientId(GOOGLE_CLIENT_ID);
    $googleClient->setAssertionCredentials(new Google_Auth_AssertionCredentials(
        GOOGLE_CLIENT_EMAIL,
        array('https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'), 
        file_get_contents(GOOGLE_CLIENT_KEY_PATH),  // Why can't I use the hard-coded GOOGLE_CLIENT_KEY here?
        G_CLIENT_KEY_PW
    ));
    //$googleClient->setAccessType('offline');
    // get an accessToken
    try
    {
        $googleClient->getAuth()->refreshTokenWithAssertion();  // the problem is here, on this line
    }
    catch (Google_Auth_Exception $exception)
    {
        echo $exception->getMessage();
    }
    $accessToken = json_decode($googleClient->getAccessToken());
    $accessToken = $accessToken->access_token;
?>

For some reason, the statement in the try block is throwing Exception: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' .

What the heck am I doing wrong? What more do I need to do, so that this works??

回答1:

I think I found the answer to my question here: https://github.com/asimlqt/php-google-spreadsheet-client/issues/24

/* They, for some reason, had trouble on the line CURLOPT_SSL_VERIFYPEER => true (and fixed it by setting that index to false) */

Also, echo $SpencerWieczorek->getResponse($this->getQuestion());