Laravel 5, Socialite和google/apicclient包
源代码:https://github.com/svpernova09/meatings
我有socialite工作很好与谷歌登录用户。
我试图在不同的方法中调用API来获取日历事件作为登录用户。我被在哪里使用什么标记所困扰。
得到的错误是:
"error":"invalid_grant",
"error_description": "代码已被赎回。"
我的方法:
public function show($user_id)
{
$user = $this->user->find($user_id);
$client = new Google_Client();
$client->setClientId(env('GOOGLE_CLIENT_ID'));
$client->setClientSecret(env('GOOGLE_CLIENT_SECRET'));
$client->setRedirectUri(env('GOOGLE_CALLBACK_URL'));
$client->authenticate($user->code);
$service = new Google_Service_Calendar($client);
$calendarId = $user->calendar_id;
$optParams = array(
'maxResults' => 5,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = [];
if (count($results->getItems()) > 0) {
foreach ( $results->getItems() as $event ) {
$events[] = $event;
}
}
return view('users.calendar')->with('events', $events);
}
Laravel的输出是:
Google_Auth_Exception在OAuth2.php行127:获取OAuth2访问令牌错误,消息:'invalid_grant'
$user-code看起来像:4/hfPzDrOHyaEv3asdfadsfasdfDw72nXIl0rt2boUpGj7hM。Um5wPgCeasdfasdfafd1t6qxkmwI
任何指导将是伟大的。用谷歌指南我什么也找不到
日历使用的范围与社交登录不同。你得再请求一次许可。您可以查看日历API的文档,并在Google Playground上试用。
Google还返回"代码已被赎回"。,因为您已经有一个访问令牌,可以使用您的代码访问用户的公共信息。