Wrote this stored procedure:

var q = Args.q; var count = Args.count; var groups = API.groups.search({ "q": q, "count": count }); var result = []; if (groups.length == 0) { return []; } else { var i = 0; while (i < groups.items.length) { var members = API.groups.getMembers({ "group_id": groups.items[i].id, "count": 0 }).count; result = result + [{ "group": groups.items[i], "members": members }]; i = i + 1; } } return result; 

It returns a list of found groups along with the number of people in this group. In the web environment of VK it works fine.

I try to use it in my application:

 https://api.vkontakte.ru/method/execute.getSourceInfo?access_token=' + self.accessToken + 'q=' + name + '&count=3' + '&callback=JSON_CALLBACK 

I get the error:

"User authorization failed: invalid access_token (4)."

And this is despite the fact that all the methods that are used in the procedure, when called directly from the application, work perfectly.

What kind of magic?

Update 10/13/2016

The line with the method call is now formed like this:

 'https://api.vk.com/method/execute.getSourceInfo?access_token=' + self.accessToken + '&q=' + name + '&count=3' + '&version=5.57&callback=JSON_CALLBACK' 

But the stored procedure is still not executed. Errors:

 error_code: 7 error_msg: "Permission to perform this action is denied" method: "groups.search" error_code: 7 error_msg: "Permission to perform this action is denied" method: "execute.getSourceInfo" 

I also suggested that you need to explicitly pass the token to the groups.search method, but this did not help either:

 var q = Args.q; var count = Args.count; var access_token = Args.access_token; var groups = API.groups.search({ "q": q, "count": count, "access_token": access_token, }); ... 

In general, I do not understand what the problem is.

Updated on 10/14/2016

It was possible to get access to the execution of the stored procedure. On the advice of @terron, requested access to groups. Now the following problem has arisen - the response is empty when called on the client. When called in a VC environment, it works fine:

 var q = Args.q; var count = Args.count; var groups = API.groups.search({ "q": q, "count": count }); var result = []; if (groups.length == 0) { return []; } else { // Все, что добавляется в results в этом цикле - не доступно при вызове. // При чем, если вызывать процедуру в окружении ВК - все работает. var i = 0; while (i < groups.items.length) { var members = API.groups.getMembers({ "group_id": groups.items[i].id, "count": 0 }).count; result = result + [{ "group": groups.items[i], "members": members }]; i = i + 1; } return result; } 

I call the procedure as follows (AngularJS):

 var findContentSource = function(name) { var self = this; return $q(function(resolve, reject) { $http.jsonp( 'https://api.vk.com/method/execute.getSourceInfo?q=' + window.encodeURIComponent(name) + '&count=3&version=5.58&callback=JSON_CALLBACK&access_token=' + self.accessToken ).success(function(data) { console.log(data); }); }); } 

    1 answer 1

    You lost & when concatenating a token with a string. Your string:

     https://api.vkontakte.ru/method/execute.getSourceInfo?access_token=' + self.accessToken + 'q=' + name + '&count=3' + '&callback=JSON_CALLBACK 

    It should be like this:

     https://api.vkontakte.ru/method/execute.getSourceInfo?access_token=' + self.accessToken + '&q=' + name + '&count=3' + '&callback=JSON_CALLBACK 

    Also, do not forget to specify the API version in the v parameter and use api.vk.com as the API api.vk.com and not api.vkontakte.ru - this endpoint is considered obsolete and will soon be disabled .

    • Thanks for the answer, corrected the errors you indicated, but it did not help. Your question has been updated. - Ilya Bizunov
    • When authorizing a user, did you try to request access to communities? - neluzhin
    • hmm, no where is it written that access to communities is needed, but it solved the problem with access. True, now ALWAYS comes with an empty result. - Ilya Bizunov
    • When you pass the q parameter to your execute method, do you do URL encoding? Your method starts perfectly with me both from the web environment of VKontakte and when accessing it via the API. - neluzhin
    • one
      The problem is definitely in your Angular code. I, unfortunately, do not understand Angular, but the example of JSONP-request on Angular found in the vast Stack Overflow was a working one. Here is an adapted version, just substitute your working token: jsfiddle.net/terron/r0yhf0gx/5 - everything works. - neluzhin