YooBalance/auth.php

58 lines
1.9 KiB
PHP
Raw Normal View History

<?php
session_start();
// Конфигурация приложения
$client_id = 'вставьте свой ключ приложения';
$redirect_uri = 'https://ваш путь к скрипту/auth.php'; // URL, куда ЮMoney перенаправит после авторизации
$scope = 'account-info operation-history operation-details';
// Шаг 1: Перенаправить пользователя на страницу авторизации
if (!isset($_GET['code'])) {
$auth_url = "https://yoomoney.ru/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect_uri}&scope={$scope}";
header('Location: ' . $auth_url);
exit();
}
// Шаг 2: Обработать редирект и получить токен
if (isset($_GET['code'])) {
$code = $_GET['code'];
$token_url = "https://yoomoney.ru/oauth/token";
// Параметры запроса
$data = array(
'code' => $code,
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
);
// Выполнение POST запроса для получения токена
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($token_url, false, $context);
if ($result === FALSE) {
die('Ошибка при получении токена');
}
$response = json_decode($result, true);
if (isset($response['error'])) {
die('Ошибка: ' . $response['error_description']);
}
$access_token = $response['access_token'];
echo "Ваш access token: " . $access_token;
}
?>