I use OpenAPI , but I can not get information about the user. Name, surname and ID. Tell me how to do it
Answer 1, Authority 100%
To begin with, you need to connect OpenAPI.js
so:
& lt; script src = "// vk.com/js/api/openapi.js" Type = "text / javascript "& gt; & lt; / script & gt;
Next, you must initialize the application with your identifier in Body
:
& lt; script type = "text / javascript" & gt;
Vk.init ({
APIID: 666.
});
& lt; / script & gt;
Next, authorized. If successfully logged in, then the user’s data already come to answer
vk.auth.login (function (response) {
if (response.session) {
showauthdata (response.session);
If (response.settings) {
Console.log (response.settings);
// Selected user access settings If they were requested
}
} else {
// user pressed the Cancel button in the authorization window
}
});
Function ShowauthData (DATA) {
$ ('Body'). Append ('& lt; P & GT; EXPIRE:' + DATA.EXPIRE + '& lt; / p & gt;');
$ ('Body'). Append ('& lt; p & gt; MID:' + Data.mid + '& lt; / p & gt;');
$ ('Body'). Append ('& lt; p & gt; fio:' + data.user.first_name + '' + data.user.last_name + '& lt; / p & gt;');
$ ('BODY'). Appendomain: '+ Data.user.domain +' & lt; / p & gt; ');
$ ('Body'). Append ('& lt; P & GT; -------------------------------------- ----------- & lt; / p & gt;);
}
If you want to separately contact any API
method after authorization, then you need to write like this:
vk.api.call (Method, Params, Callback);
where
- Method (String) – Title
API
; - Params (Object) – method parameters;
- Callback – callback function.
For example:
vk.api.call ('users.get', {user_ids: 1}, function (R) {
if (r.Response) {
Alert ('Hi,' + r.Response [0] .first_name);
}
});
This will cause the users.get – and get the user data with the 1
Answer 2
First thing you need Register your VK (Type Specify “Website”) – & nbsp; and copy an application ID:
In the settings of your application, you must specify the domain from which your code will work. If it is copied to another domain – & nbsp; it will stop working.
in VK documentation for OpenAPI There are examples of code. Here they are with minor changes:
& lt; div id = "b-user" & gt; & lt; / div & gt; & lt;! - Here you will bring the username - & gt;
& lt; script src = "// vk.com/js/api/openapi.js"> ;< ;/script> & lt;! - OpenAPI script - & gt;
and javascript:
vk.init ({apiid: 5599725}); // registered a VK application
// here: https://vk.com/editapp?Act=Create
// In the settings you need to specify your domain, where the code will work
Vk.Auth.login (Function (Response) {
var el = document.getelementByid ('B-User');
if (response.session) {
/ * User successfully authorized * /
If (response.session.mid & amp; & amp; response.session.user) {
el.innerhtml = 'hello, & lt; a href = "https://vk.com/id%UID%" target = "_ blank" & gt;% username% & lt; / a & gt;!'
.replace ('% Username%', response.session.user.first_name + '' + response.session.user.last_name)
.replace ('% uid%', response.session.user.id);
} else {
el.innerhtml = 'User data did not come something';
}
} else {
/ * User pressed the Cancel button in the authorization window * /
el.innerhtml = 'the user did not agree ";
}
});