M
M
Mosapi2017-04-23 00:38:31
PHP
Mosapi, 2017-04-23 00:38:31

Why doesn't oauth authorization work until the end?

I'm trying to figure out the code from one tutorial.:

<?php

    $client_id = ''; // ID приложения
    $client_secret = ''; // Защищённый ключ
    $redirect_uri = 'http://мой сайт.com/system/auth/vk.php'; // Адрес сайта

    $url = 'http://oauth.vk.com/authorize';

    $params = array(
        'client_id'     => $client_id,
        'redirect_uri'  => $redirect_uri,
    'display' => 'popup',
        'response_type' => 'code'
    );

    echo $link = '<p><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Аутентификация через ВКонтакте</a></p>';

if (isset($_GET['code'])) {
  //echo $_GET['code'];
    $result = false;
    $params = array(
        'client_id' => $client_id,
        'client_secret' => $client_secret,
        'redirect_uri' => $redirect_uri,
    'code' => $_GET['code']
    );
/* ДАЛЬШЕ ЭТОГО МЕСТА НЕ ИДЕТ**/
    $token = json_decode(file_get_contents('https://oauth.vk.com/access_token' . '?' . urldecode(http_build_query($params))), true);

    if (isset($token['access_token'])) {
        $params = array(
            'uids'         => $token['user_id'],
            'fields'       => 'uid,first_name,last_name,screen_name,sex,bdate,photo_big',
            'access_token' => $token['access_token']
        );

        $userInfo = json_decode(file_get_contents('https://api.vk.com/method/users.get' . '?' . urldecode(http_build_query($params))), true);
        if (isset($userInfo['response'][0]['uid'])) {
            $userInfo = $userInfo['response'][0];
            $result = true;
        }
    }

    if ($result) {
        echo "Социальный ID пользователя: " . $userInfo['uid'] . '<br />';
        echo "Имя пользователя: " . $userInfo['first_name'] . '<br />';
        echo "Ссылка на профиль пользователя: " . $userInfo['screen_name'] . '<br />';
        echo "Пол пользователя: " . $userInfo['sex'] . '<br />';
        echo "День Рождения: " . $userInfo['bdate'] . '<br />';
        echo '<img src="' . $userInfo['photo_big'] . '" />'; echo "<br />";
    }
}
?>

As a result: I click on the link, log in, everything seems to be fine. I get code and stay on my resource with a link of this kind in the address bar
http://мой ресурс/system/auth/vk.php?code=2a6хх7b5d19хх0933b

And it doesn't go any further. Moreover, if you manually create a link, both access_token and standard parameters are displayed.
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-04-25
@Mr_Saxa

Well, I don't understand much about this:
Maybe I don’t understand something, but it seems that the token comes in the address bar and not in the server response.
But maybe there's something I don't understand

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question