D
D
deitel2014-04-13 20:20:07
In contact with
deitel, 2014-04-13 20:20:07

How to register and authorize in vk.com api?

Hello!
There is some application for mobile devices (android, ios), in which you need to make authorization through the vk.com social network.
I re-read a bunch of documentation on api vk, but nothing came of it.
It says to create an application in your profile on the vk.com website, I already have it ready, namely the parameters:
client_secret, client_id, code.
The logic of work should be as follows:
1) The client enters the application on a mobile phone, presses authorization using VK.
2) Next, the data is sent to our server (website / handler), where the php script checks for the presence of this user_id vkontakte of the user, and, accordingly, if not, adds it to the database with all tokens, etc., if there is, simply authorizes.
As an example, I post a script where registration / login is implemented only for Facebook, I need to do something similar but only for vk.com.

public function action_fblogin()
    {
        $token = $this->request->query('access_token');
        if ($token && is_string($token))
        {
            $response = json_decode(
                $this->create_request('https://graph.facebook.com/me/?access_token='.$token.'&locale=ru_RU')
                    ->execute()
                    ->body()
            );

            if (isset($response->error)) {
                $this->response_json = array('fb_error' => $response->error);
                return;
            }

            if (!isset($response->id)) {
                $this->response_json = array('error' => 'Не удалось получить ID учётной записи в Facebook');
                return;
            }

            $_email = isset($response->email) ? $response->email : '';

            $user = ORM::factory('User')->where('facebook_id', '=', $response->id);

            if ($_email)
                $user = $user->or_where('email', '=', $_email);

            $user->find();

            if ($user->loaded()) {
                $this->response_json = array(
                    'user_id'   => $user->id,
                    'token'     => $user->create_token($this->request->headers('User-Agent')),
                    'name'      => $user->name,
                    'email'     => $user->email,
                    'gender'    => $user->gender,
                    'phone'     => $user->phone,
                    'avatar'    => $user->avatar->full_url(),
                );
            }
            else {
                $_gender = 0;
                if (isset($response->gender)) {
                    if ($response->gender == 'male' || $response->gender == 'мужской')
                        $_gender = 1;
                    elseif ($response->gender == 'female' || $response->gender == 'женский')
                        $_gender = 2;
                }
       }
}

I hope I explained it clearly, any help would be greatly appreciated!
Ps I'm using Kohana as a framework, if that helps.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2014-04-13
@sanchezzzhak

'http://api.vk.com/oauth/authorize?client_id=' . $config['vk']['app_id'] .
            '&response_type=code'. 
            '&scope=notify,friends'.
            '&redirect_uri=' . urldecode( $config['vk']['redirect_url'])

We transfer to the VK site that presses the button to log in and allows rights
Further on your site from Get variables we get CODE and usser_id
public function http($url){
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_USERAGENT, 'dev-php');
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ci, CURLOPT_TIMEOUT, 20);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt($ci, CURLOPT_HEADER, false);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response = curl_exec($ci);
        curl_close($ci);
        return $response;
    }

    /**
     * Получить по коду access_token
     * @param  string $code секретный код от пользователя...
     * @return  array|false  получаем данные в виде массива или false
     **/
    public function getAccessToken($code){
        $response = json_decode($this->http('https://api.vk.com/oauth/access_token?client_id=' . $this->app_id .
        '&client_secret=' . $this->app_key . '&code=' . $code . '&redirect_uri='.  urldecode($this->app_redirect_url)   ),true);
        return $response;
    }

++ Probably you need to ask for the level of rights offline (eternal token)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question