E
E
EVOSandru62016-08-05 13:52:30
PHP
EVOSandru6, 2016-08-05 13:52:30

How to leave an entry on the wall of the Vkontakte group on behalf of an authorized user using the VK API?

Good afternoon,
is there a way to send a post to the group wall on behalf of the user. in an application like Website or StandAlone ? I have both created. StandAlone with a static Token. For the Website, the token changes after each authorization.
I
1. authorize the user on the test site
2. remember the ACCESS_TOKEN in the session
3. try to make an entry (review to the group on behalf of the authorized user is implied).
Unfortunately, in StandAlone , you can only send records through the API on behalf of the application owner.
I want to leave entries from an authorized user. Which is already authorized by meansVKontakte API like this:

if(!$this->code){
            die('Неверный код');
        }
        $curl   = curl_init();
        $request_params = [
            'client_id'         =>      APP_ID,
            'client_secret'     =>      APP_SECRET,
            'code'              =>      $this->code,
            'redirect_uri'      =>      REDIRECT_URI,
        ];
        curl_setopt($curl, CURLOPT_URL, URL_ACCESS_TOKEN.'?'. http_build_query($request_params));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($curl);
        curl_close($curl);
        $ob = json_decode($result);
        if($ob->access_token){
            $this->setToken($ob->access_token);
            $this->setUid($ob->user_id);
            return true;
        }
        elseif($ob->error) {
            $_SESSION['error']  =  "ERROR";
            return false;
        }

Then I just pull the user data by another method.
Thus, I tried to send a message (as a result, you need to write to the wall, but the meaning does not change)
public function sendMessage($description){
        $access_token    =   $_SESSION['access_token'];
        $request_params     =   [
            'user_id'       =>  22557780,
            'random_id'     =>  mt_rand(20, 99999999), 
            'peer_id'       =>  22557780,
            'domain'        =>  'mozart',
            'chat_id'       =>  36,
            'message'       =>  $description,
            'v'             =>  '5.52',
            'access_token'  =>  $access_token
        ];
        $url    =  'https://api.vk.com/method/messages.send?'.http_build_query($request_params);
        printArr(json_decode(file_get_contents($url)));
        die();
        $this->redirect('http://qas.loc');
    }

I catch an error:
[error_code] => 15
[error_msg] => Access denied: no access to call this method .

Help good people!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Flaks, 2016-08-06
@EVOSandru6

To be honest, I did not really understand the essence of the issue, so I will go through all the possible points at once.
Through standalone applications, you can send records on behalf of any user who has authorized through your application.
The messages .* methods can only be used through standalone applications. For websites and iFrame applications, posting via the wall.post method is available only through the confirmation window, and for standalone applications - without any problems.

I catch an error:
[error_code] => 15
[error_msg] => Access denied: no access to call this method .

You are forgetting to pass the scope parameter when getting the token. See more about access rights here .
And one more thing: you will not be able to work with the user's messages if you work with the user through the website. These methods are supported only by standalone applications that need to be used in desktop and mobile applications or in browser extensions. It will be possible to authorize a user in a standalone application through a browser, but you won’t be able to get a token either through PHP or JavaScript, unless you ask the user to copy the token manually, which contradicts the basics of UX, because. on the page for obtaining a token it is written that the user should not copy it, because it is not safe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question