- Автор темы
- #1
Добрый день.
Помогите как пройти авторизацию.
Жму на сссылку - Аутентификация через Google
Выдает ошибку
400. That’s an error.
Error: redirect_uri_mismatch
Application: ssu
You can email the developer of this application at: w@gmail.com
The redirect URI in the request: Для просмотра ссылки Войдиили Зарегистрируйся did not match a registered redirect URI.
Learn more
Request Details
That’s all we know.
код
Помогите как пройти авторизацию.
Жму на сссылку - Аутентификация через Google
Выдает ошибку
400. That’s an error.
Error: redirect_uri_mismatch
Application: ssu
You can email the developer of this application at: w@gmail.com
The redirect URI in the request: Для просмотра ссылки Войди
Learn more
Request Details
That’s all we know.
код
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title>Аутентификация через Google</title>
</head>
<body>
<?php
$client_id = '1081633000000ххххххauh65de.apps.googleusercontent.com'; // Client ID
$client_secret = 'ZQXхххххххMvz2-7'; // Client secret
$redirect_uri = 'http://localhost:4567/'; // Redirect URIs
$url = 'https://accounts.google.com/o/oauth2/auth';
$params = array(
'redirect_uri' => $redirect_uri,
'response_type' => 'code',
'client_id' => $client_id,
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
);
echo $link = '<p><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Аутентификация через Google</a></p>';
if (isset($_GET['code'])) {
$result = false;
$params = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code',
'code' => $_GET['code']
);
$url = 'https://accounts.google.com/o/oauth2/token';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$tokenInfo = json_decode($result, true);
if (isset($tokenInfo['access_token'])) {
$params['access_token'] = $tokenInfo['access_token'];
$userInfo = json_decode(file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo' . '?' . urldecode(http_build_query($params))), true);
if (isset($userInfo['id'])) {
$userInfo = $userInfo;
$result = true;
}
}
if ($result) {
echo "Социальный ID пользователя: " . $userInfo['id'] . '<br />';
echo "РРјСЏ пользователя: " . $userInfo['name'] . '<br />';
echo "Email: " . $userInfo['email'] . '<br />';
echo "Ссылка на профиль пользователя: " . $userInfo['link'] . '<br />';
echo "Пол пользователя: " . $userInfo['gender'] . '<br />';
echo '<img src="' . $userInfo['picture'] . '" />'; echo "<br />";
}
}
?>
</body>
</html>