【问题标题】:Trying to Get Google accessToken尝试获取 Google accessToken
【发布时间】:2014-11-17 04:00:04
【问题描述】:

看来,无论我做什么,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 块中的语句抛出 Exception: Error refresh the OAuth2 token, message: '{ "error" : "invalid_grant" }' 。

我到底做错了什么?我还需要做些什么才能使它起作用??

【问题讨论】:

  • 另外,key.p12 文件可能不包含我的实际密钥,因为它是在 Composer 从 Internet 下载的文件夹中找到的。我将如何获得所需的 p12 文件?
  • 那你可能want to generate a new P 12 key。并使用您创建的密钥。下载该密钥并使用您下载到的路径。

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


【解决方案1】:

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

/* 他们出于某种原因在CURLOPT_SSL_VERIFYPEER =&gt; true 线上遇到了麻烦(并通过将该索引设置为false 来修复它)*/

另外,echo $SpencerWieczorek-&gt;getResponse($this-&gt;getQuestion());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2020-04-21
    • 2013-11-08
    • 2020-02-07
    • 2022-01-28
    • 1970-01-01
    相关资源
    最近更新 更多