D
D
Dmitry Svetov2016-08-02 18:39:24
JavaScript
Dmitry Svetov, 2016-08-02 18:39:24

VKontakte API method messages.get. What am I doing wrong?

Good day to all. This is my first time dealing with social media APIs. I'm trying to use the VK API. In this case, the messages.get method. Got APP_ID wrote code with client authorization and messages.get request:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="//vk.com/js/api/openapi.js" type="text/javascript"></script>
</head>
<body>
  <div id="vk_api_transport"></div>
  <div id="login_button" onclick="VK.Auth.login(authInfo);"></div>
  <script type="text/javascript">
    window.vkAsyncInit = function() {
      VK.init({
        apiId: APP_ID
      });
    };

    function authInfo(response) {
      console.log(response)
      if (response.session) {
        alert('user: ' + response.session.mid);
      } else {
        alert('not auth');
      }
    }
    VK.Auth.getLoginStatus(authInfo);
    VK.UI.button('login_button');

    VK.Api.call('messages.get', {
      fields: 'messages'
    }, function(data) {
      console.log(data)
    });

    setTimeout(function() {
      var el = document.createElement("script");
      el.type = "text/javascript";
      el.src = "//vk.com/js/api/openapi.js";
      el.async = true;
      document.getElementById("vk_api_transport").appendChild(el);
    }, 0);
  </script>
</body>
</html>

As a result, I get the following message in the console:
Access denied: no access to call this method

Tell me what I did wrong and how to fix it, where to look?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Flaks, 2016-08-02
@shell_bug

The messages .* methods can only be accessed by standalone applications. These are applications that work without the help of a browser. For example, clients (official VK apps, Kate Mobile, Snapster, etc.). And you have an application type of "website".
There's nothing you can do about it, and you won't be able to get the Open API to work with messages. The only way out is to throw the user on the page for obtaining a token in your standalone application and ask them to copy it into a form on your site, and then make requests from the server with this token. But this has a bad effect on UX, if only because on the page from where the user will copy this very token, it is written in bold that copying the token may be unsafe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question