【发布时间】:2014-11-17 04:00:04
【问题描述】:
看来,无论我做什么,Google 都在尽最大努力阻止我完成这个研究项目。我的项目让我使用 Google 电子表格作为数据库,并使用所述电子表格中的数据执行程序化的 Google 图片搜索,并向最终用户显示一些结果。
设置说明
我已开始按照此处的说明进行操作:https://github.com/asimlqt/php-google-spreadsheet-client。我将Composer 下载到我的计算机上,然后按照说明here 和here 让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