试图获取Google accessToken

时间:2014-11-17 04:00:05

标签: php google-api google-sheets google-oauth

似乎无论我做什么,Google都会尽力阻止我完成这项研究项目。我的项目让我使用Google电子表格作为数据库,并使用所述电子表格中的数据执行程序化的Google图片搜索,并向最终用户显示一些结果。

设置说明

我已按照此处的说明开始:https://github.com/asimlqt/php-google-spreadsheet-client。我将Composer下载到了我的计算机,然后按照说明herehere让Composer将必要的库下载到我的项目中。

编码过程 从那里,我试图复制this guy在答案中所做的事情。实际上,这是我的代码:

<?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;
?>

由于某种原因,try块中的语句抛出异常:刷新OAuth2令牌时出错,消息:&#39; {&#34; error&#34; :&#34; invalid_grant&#34; }&#39; 。

我到底做错了什么?我还需要做什么,这样才有效?

1 个答案:

答案 0 :(得分:0)

我想我在这里找到了问题的答案:https://github.com/asimlqt/php-google-spreadsheet-client/issues/24

/ *由于某些原因,他们在第CURLOPT_SSL_VERIFYPEER => true行遇到了问题(通过将该索引设置为false来修复它)* /

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