📜 ⬆️ ⬇️

Sign in with VK

How many percent of your site users are also VK users? How do you think, how many people do not write / comment / add (underline the necessary), just because they are too lazy to register on another incomprehensible site for the 100th time? I propose to answer for yourself these 2 questions before scolding VKontakte and the Open API.

In my case, about 1,500 people come to the site from VKontakte every day and it seems to me that it is my duty to provide them with a simple and convenient way of registering.

It is unlikely that I will discover something new, but if at least 1 more site becomes a little more convenient after this article, it means that I have not wasted my time writing this text.

I wrote a module Open API authorization for a site in PHP, so all the code will be on it. For Django there was also an article .
')
The source code for the simplest example is available on github. I will not reprint it here, just briefly tell you how it works.

The main work is performed by the class Auth_Vkontakte, containing 3 methods:

Of course, you will also have to add vkontakte support to your site engine, for example, add the “vk_id” field to the user's plate in which the VK user ID will be stored.

The algorithm of work in real conditions is approximately as follows:
  1. After the user enters Vkontakte, we transfer him to a separate page (in my case it is vk.php), where we check whether the user is already registered with such a vk_id. If not, we show him a simplified registration form (for example, only a login and email). After that we create a user and transfer the newly registered visitor to the main page.
  2. Create a backend that will check for a cookie on VK via Auth_Vkontakte :: is_auth, in case the visitor has not entered your site in any way. If is_auth returns a value other than false and a user with such an id is registered on your site - we authorize users through your system. If suddenly it turned out that it is not registered - we again show the registration form.
  3. When logout we delete, in addition to your cookies, a cookie from VKontakte (Auth_Vkontakte :: logout method)

In general, it seems to me, any documentation on any API should begin with similar examples. VKontakte developers, however, do not think so and it is impossible to understand the Open API from their documentation. In fact, I did not use it - I only read everything in the source code on durov.at.
Surely the VKontakte company cannot afford to hire a normal technical writer who would write examples and normal manuals, and not dry lists of functions with a brief description of the parameters that it accepts?

In order to try a test example in work (which, however, will not show anything except your ID), just download the source code and set up a local web server to the address: openapi.lc (add to hosts: 127.0.0.1 openapi.lc).

If someone liked and became useful - I will also write an article about FacebookConnect.

Source: https://habr.com/ru/post/92518/


All Articles