看起来,无论我做什么,谷歌都在竭尽全力阻止我完成这个研究项目。我的项目让我使用 Google 电子表格作为数据库,并使用所述电子表格中的数据执行程序化的 Google 图片搜索,并向最终用户显示一些结果。
设置说明
我开始按照此处的说明进行操作:https://github.com/asimlqt/php-google-spreadsheet-client https://github.com/asimlqt/php-google-spreadsheet-client。我下载了Composer https://getcomposer.org/doc/00-intro.md#installation-windows到我的计算机,然后按照说明进行操作here https://github.com/asimlqt/php-google-spreadsheet-client#installation and here https://developers.google.com/api-client-library/php/start/installation让 Composer 将必要的库下载到我的项目中。
编码过程从那里开始,我尝试复制什么this guy https://stackoverflow.com/questions/21470405/google-api-how-to-connect-to-receive-values-from-spreadsheet他的回答中正在做。事实上,这是我的代码:
<?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: Errorfreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' 。
我到底做错了什么?我还需要做什么才能使其发挥作用?