R
R
Ruslan Dorofeev2018-12-06 15:43:32
JavaScript
Ruslan Dorofeev, 2018-12-06 15:43:32

Why doesn't the VK Client API give access rights to methods with a community access key?

In the VKontakte community application, I am trying to call methods that are marked as available with the access key for the group. According to the documentation, I have to call the method for requesting access rights using the Client API method, and then calling the VK API method itself:

VK.init(function() {
  $('#item_addserver').on('click', function() {
    // Запрашиваем права доступа
    accessGroup();
  });
});

function accessGroup() {
  VK.callMethod("showGroupSettingsBox", 262144);
  VK.addCallback('onGroupSettingsChanged', function(){ 
    //alert("Успешно"); 
    // Получаем информацию о серверах в группе
    showServers();
  });

  VK.addCallback('onGroupSettingsCancel', function(){ 
    alert("Отказ в доступе"); 
  });
}

function showServers() {
  VK.api("groups.enableOnline", 
  {
    "group_id": the_group_id,
    "v": the_version,
    "test_mode": the_test_mode
  }, 
  function (data) {
    var res = JSON.stringify(data);
    alert(res); 
  });
}

The request for rights appears, I click, successfully, groups.enableOnline (or similar) is called, and then there is an access error:
5c09191fbd708358231361.png
I only have an assumption that because of test_mode=1, but I saw somewhere in the documentation that it can be called, in the test.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Dorofeev, 2018-12-06
@rusld

Actually, I found a solution. And, as it were, even more logical than my first plan for organizing a request to the VK api. I initially assumed that after allowing access rights to the group by the administrator, the access token on behalf of the group is returned and stored somewhere in the Client API, and thus, when calling functions that require it, it is substituted automatically. But my vanilla dreams failed. After receiving the key, it must be put into action by transferring it to the server from which the application is launched, and there already calling the VK API:
Client (JS) method:

VK.init(function() {
  $('#item_addserver').on('click', function() {
    // Запрашиваем права доступа
    accessGroup();
  });
});

function accessGroup() {
  VK.callMethod("showGroupSettingsBox", 262144);
  VK.addCallback('onGroupSettingsChanged', function(settings, grp_token){ 
    addServer(grp_token); 
  });

  VK.addCallback('onGroupSettingsCancel', function(){ 
    alert("Отказ в доступе"); 
  });
}

// Добавление сервера
function addServer(gtoken) {
  $.get('https://mymo.info/api.php', 
    {
      "viewer_id": the_viewer_id,
      "secret": the_secret,
      "method": "add_server",
      "group_access_token": gtoken,
      "group_id": the_group_id
    },
    function(data) {
      alert('addserver: ' + data);
    });
}

Server (PHP):
// ... дополнительная обработка подключений к своему API
/* создает новый сервер */
  function add_server() {
    //print_r($_GET);
    $access_token = clearStr($_GET['group_access_token']);
    $group_id = clearInt($_GET['group_id']);
    
    //print "$group_id\n$access_token";
    
    $api_params = array(
      "group_id" => $group_id,
      "access_token" => $access_token,
      "v" => VK_API_VERSION
    );
    
    $get_query_string = http_build_query($api_params);
    $api_request = 'https://api.vk.com/method/groups.getCallbackServers?'.$get_query_string;
    
    //print $get_query_string;
    
    $api_result = file_get_contents($api_request);
    
    print $api_result;
  }

Now it works :)
5c0944e3cf23a677455306.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question