Answer the question
In order to leave comments, you need to log in
vk.com api. How to make requests on the server with a custom token?
Good afternoon!
The user logs in to VKontakte via oauth, on the server I receive a token according to the documentation for the Authorization Code Flow scheme. The problem is that when I try to execute any vk api method, for example photos.get, I get Error 5. User authorization failed: method is unavailable with server auth. I understand that the error says that this method can only be performed from under the client.
Is it possible to make requests on the server side using a token? Specifically, I need to get the id of the current user and a list of his photos on the server.
Answer the question
In order to leave comments, you need to log in
You need to do this in the script through which he passes authorization, and write all the results to the database
<?php
$client_id = '3485070'; // ID приложения
$client_secret = 'lYjfUZwZmlJJlFIqQFAj'; // Защищённый ключ
$redirect_uri = 'http://localhost/vk-auth'; // Адрес сайта
$url = 'http://oauth.vk.com/authorize';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
);
echo $link = '<p><a href="' . $url . '?' . urldecode(http_build_query($params)) . '">Аутентификация через ВКонтакте</a></p>';
if (isset($_GET['code'])) {
$result = false;
$params = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $_GET['code'],
'redirect_uri' => $redirect_uri
);
$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 />";
}
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question