The library itself is a single vk_api object and the md5 function, honestly borrowed from Webtoolkit. The vk_api object is not ready for use by itself - it is necessary to create an instance from it using the constructor:<!-- , HTML- --> < script src ="vk_api.js" type ="text/javascript" ></ script > * This source code was highlighted with Source Code Highlighter .
<!-- , HTML- --> < script src ="vk_api.js" type ="text/javascript" ></ script > * This source code was highlighted with Source Code Highlighter .
<!-- , HTML- --> < script src ="vk_api.js" type ="text/javascript" ></ script > * This source code was highlighted with Source Code Highlighter .
<!-- , HTML- --> < script src ="vk_api.js" type ="text/javascript" ></ script > * This source code was highlighted with Source Code Highlighter .
<!-- , HTML- --> < script src ="vk_api.js" type ="text/javascript" ></ script > * This source code was highlighted with Source Code Highlighter .
In principle, it would be possible to perform all initialization when loading the vk_api.js script itself , but this is not the way of a samurai - what if you don’t have to call the API in this session at all? In addition, during initialization, the constructor is passed parameters that somehow need to be set somewhere and passed to the script.* This source code was highlighted with Source Code Highlighter .
- var api = new vk_api ( / * parameters * / );
As you can see, everything is very simple. The designer is supposedly given a secret key (aha, the secret one is presented in the page code in an open form) and two functions, the first of which is performed in case of successful initialization and the other in case of an error during initialization. In fact, the second function is not at all necessary if the developer does not want to take into account those isolated cases when an error occurs during initialization. In addition, there is another parameter, which is a normal logical flag, indicating whether the test mode is enabled.* This source code was highlighted with Source Code Highlighter .
- // Create an object from vk_api (). All further work must take place.
- // with api object only.
- var api = new vk_api (
- // This is a Secret, it is listed on the application editing page
- 'hGtsH5G0' ,
- // This function will be called if the initialization of the API is successful.
- // Good place to set all callback functions through
- // addCallback ()
- function () {
- // Call the change settings window with the marked settings for
- // access to friends and to the wall. User will not be able to close
- // called window until it sets the required settings.
- api.makeSettings (api.SETT_FRIENDS | api.SETT_WALL);
- // Add a function that will run if the user adds
- // application to your page.
- api.addCallback (
- 'onApplicationAdded' ,
- function () {
- //actions
- }
- );
- },
- // This function is called if the API initialization failed.
- // In this case, it is impossible to use external (external) calls.
- function () {
- // error message
- }
- );
Let us turn, finally, to why the application was developed, i.e., directly to the call of API functions. For this, the call method is provided:* This source code was highlighted with Source Code Highlighter .
- // Display a window asking to install the application.
- api.makeInstall ();
- api.addCallback (
- 'onApplicationAdded' ,
- // This function will be called when the user installs the application.
- function () {
- $ ( '#text' ) .append ( 'Application added \ n' );
- // Ask the user to allow the application to access
- // to friends and to post on the walls
- api.makeSettings (api.SETT_FRIENDS | api.SETT_WALL);
- api.addCallback (
- 'onSettingsChanged' ,
- // The function will be executed when the user changes the application settings.
- function (settings) {
- $ ( '#text' ) .append ( 'New parameters:' + String (settings) + '\ n' );
- }
- );
- }
- );
As you can see, everything is simple, and no unnecessary actions that we need.* This source code was highlighted with Source Code Highlighter .
- // Call the API method via the call () function
- api.call (
- // Name of the API method.
- // Suppose getting extended information about profiles.
- 'getProfiles' ,
- // This is an object with API method parameters. If there are no parameters,
- // then this argument can be omitted.
- {
- uids: '4124584,2327412,1237975' ,
- // You can specify arrays that will be automatically
- // converted to strings.
- fields: [
- 'first_name' , 'last_name' , 'nickname' ,
- 'bdate' , 'city' , 'country' , 'photo'
- ]
- },
- // This function will be called when a response is received from the server.
- // data is a ready-to-use object created from a string
- // JSON received from the server.
- function (data) {
- // Actions with the data object
- }
- );
Source: https://habr.com/ru/post/81654/
All Articles