Answer the question
In order to leave comments, you need to log in
Google OAuth2.0 returns invalid_grant?
<?php
if (!empty($_GET['code'])) {
// Отправляем код для получения токена (POST-запрос).
$params = array(
'client_id' => '782016323201-ktsn3*****oogleusercontent.com',
'client_secret' => 'nbaAc****TIaCpo',
'redirect_uri' => 'https://test-server.tsecret.net/AUTH-2/redirect.php',
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'access_type' => 'offline'
);
var_dump($params);
$ch = curl_init('https://accounts.google.com/o/oauth2/token');
var_dump($ch);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data, true);
var_dump($data);
if (!empty($data['access_token'])) {
// Токен получили, получаем данные пользователя.
$params = array(
'access_token' => $data['access_token'],
'id_token' => $data['id_token'],
'token_type' => 'Bearer',
'expires_in' => 3599
);
$info = file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo?' . urldecode(http_build_query($params)));
$info = json_decode($info, true);
print_r($info);
}
}
Answer the question
In order to leave comments, you need to log in
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
$params = array(
'access_token' => $data['access_token'],
'id_token' => $data['id_token'],
'token_type' => 'Bearer',
'expires_in' => 3599
);
$params = array('access_token' => $data['access_token']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question