J
J
jadewizzard12016-05-31 18:39:47
PHP
jadewizzard1, 2016-05-31 18:39:47

What's wrong with the VK API?

Hello, I have such a problem. I'm trying to get banned users from a group using the VK API.
Here is my code:

/**
 * Created by unixwz on 5/30/16.
 */

function get_all_groups(user_id, access_token)
{
    ban_count = 0;
    $.ajax({
        type: "POST",
        url: "engine/handler.php",
        data: {"user_id": user_id,
               "access_token": access_token,
               "act" : "get_group"},
        response: "text",
        success: function (data) {
            user_groups = JSON.parse(data)["response"];
            for(i = 1; i < user_groups.length; i++)
            {
                $("#all_group").append(
                    "<br><label><input value='" + user_groups[i]["gid"] + "' id='group" + i + "' type='checkbox'>"
                    + user_groups[i]["name"] + " - " + user_groups[i]["gid"] + "</label>",
                    null);
                get_number_of_banned_users(user_groups[i]["gid"], access_token);
                // get number of banned users for all groups
            }
        }
    });
}

function get_number_of_banned_users(group_id, access_token)
{
    $.ajax({
        type: "POST",
        url: "engine/handler.php",
        data: {"group_id": group_id,
               "access_token": access_token,
               "act" : "get_banned_num"},
        response: "text",
        success: function (data) {
            alert(data);
        }
    });
}

Here is the PHP handler:
function get_all_groups($user_id, $access_token)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://api.vk.com/method/groups.get?user_id=". $user_id ."&count=1000&filter=admin,editor,moder&extended=1&access_token=". $access_token ."");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    $response = curl_exec($curl);

    return $response;
}

function get_number_of_banned_users($group_id, $access_token)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://api.vk.com/method/groups.getBanned?group_id=". $group_id ."&count=200&access_token=". $access_token ."");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    $response = curl_exec($curl);

    return $response;
}

if(isset($_POST["act"]))
{
    switch ($_POST["act"])
    {
        case "get_group":
            if(isset($_POST["user_id"]) && isset($_POST["access_token"]))
            {
                echo get_all_groups($_POST["user_id"], $_POST["access_token"]);
            }
            break;

        case "get_banned_num":
            if(isset($_POST["group_id"]) && isset($_POST["access_token"]))
            {
                echo get_number_of_banned_users($_POST["group_id"], $_POST["access_token"]);
            }
            break;
    }
}

It turns out that the first method groups.get works fine because it requires only a token without access rights, and the second method groups.getBanned always returns {"error":{"error_code":15,"error_msg":"Access denied: no access to call this method","request_params":[{"key":"oauth","value":"1"},{"key":"method","value":"groups.getBanned"}, {"key":"group_id","value":" 119484487 "},{"key":"count","value":"200"}]}} still says no access. standalone app.
<a href="https://oauth.vk.com/authorize?client_id=5485927&redirect_uri=http://localhost/&display=page&scope=groups&response_type=code">
                        <button type="button" class="btn btn-success">Авторизоваться через ВКонтакте</button>
                    </a>

Help solve the problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question