尝试获取Google accessToken [英] Trying to Get Google accessToken
问题描述
设置说明
我已经按照这里的说明开始了: https://github.com/asimlqt/php-google-spreadsheet-client 。我将作曲者下载到我的电脑,然后按照指示此处和这里让Composer将必要的库下载到我的项目中。
编码过程
从那里开始,我尝试复制这个人正在回答他的问题。事实上,这里是我的代码:
<?php
需要vendor / autoload.php;
// require'/php-google-spreadsheet-client-master\src\Google\Spreadsheet\Autoloader.php';
使用Google\Spreadsheet\DefaultServiceRequest;
使用Google\Spreadsheet\ServiceRequestFactory;
const GOOGLE_CLIENT_ID ='someClientID'; //这里,我只是从我的Google Developers帐户中提取了默认的客户端ID
const GOOGLE_CLIENT_EMAIL ='someClientEmail'; //我也在这里使用了默认的客户端电子邮件,直接从Google Developers帐户直接购买
const GOOGLE_CLIENT_KEY ='someKey'; //我使用了我用于Google自定义搜索引擎的密钥
const GOOGLE_CLIENT_KEY_PATH ='vendor'\\google\apiclient\examples\key.p12'; //我在我的项目文件夹中搜索了具有关键信息的.p12文件,并在此处使用该路径
const G_CLIENT_KEY_PW ='notasecret'; //默认(我不知道为什么我需要这样做)
//设置googleClient
$ googleClient =新的Google_Client();
$ googleClient-> setApplicationName('future-graph-611');
$ googleClient-> setClientId(GOOGLE_CLIENT_ID);
$ googleClient-> setAssertionCredentials(新Google_Auth_AssertionCredentials(
GOOGLE_CLIENT_EMAIL,
数组('https://spreadsheets.google.com/feeds','https://docs.google.com / feeds'),
file_get_contents(GOOGLE_CLIENT_KEY_PATH),//为什么我不能在这里使用硬编码的GOOGLE_CLIENT_KEY?
G_CLIENT_KEY_PW
));
// $ googleClient-> setAccessType('offline');
//获取accessToken
尝试
{
$ googleClient-> getAuth() - > refreshTokenWithAssertion(); //问题在这里,在这一行
catch(Google_Auth_Exception $例外)
{
echo $ exception-> getMessage();
}
$ accessToken = json_decode($ googleClient-> getAccessToken());
$ accessToken = $ accessToken-> access_token;
?>
出于某种原因,try块中的语句抛出异常:刷新OAuth2令牌的错误,消息:'{error:invalid_grant}'。
我做错了什么?我还需要做些什么,以便这样做?
我想我在这里找到了我的问题的答案: https://github.com/asimlqt/php-google-spreadsheet-client / issues / 24
/ *由于某种原因,它们在 CURLOPT_SSL_VERIFYPEER => true
(并通过将该索引设置为 false
)* /
, echo $ SpencerWieczorek-> getResponse($ this-> getQuestion());
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??
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());
这篇关于尝试获取Google accessToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!