J
J
Jeket2014-05-14 00:25:20
In contact with
Jeket, 2014-05-14 00:25:20

How to get email via VKontakte API?

To authorize users on the site, I created an application, placed a button on my site. How to get his email when authorizing a user?
My code:

<div id="vk_login_button" onclick="VK.Auth.login(authInfo, VK.access.EMAIL);">Войти ВКонтакте</div>
  <script type="text/javascript">
    VK.init({
      apiId: MY_APP_ID
    });

    function authInfo(response) {
      if (response.session) {
        //alert('user: '+response.session.mid);     
        getInitData();
      }
    }

    function getInitData() {
        var code;
        code = 'return {'
        code += 'me: API.getProfiles({uids: API.getVariable({key: 1280}),  fields: "photo"})[0]';
        code += '};';
        VK.Api.call('execute', { 'code': code }, onGetInitData);
    }
    function onGetInitData(data) {
        var r;
        if (data.response) {
            r = data.response;
            if (r.me) {              
                                 
                alert(r.me.first_name + ' ' + r.me.last_name + '<br/><a href="http://vkontakte.ru/id' + r.me.uid + '">  ' + r.me.photo);

            }
        }
    }

    VK.Auth.getLoginStatus(authInfo);
    VK.UI.button('vk_login_button');</script>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jeket, 2014-05-19
@Jeket

A brief algorithm of actions:
1) Register the application on VKontakte
2) Generate a link as in the API documentation https://vk.com/dev/auth_sites (if you need an email, then specify &scope=email )
3) After receiving the code, make a request to receive an access_token , the response comes with id and email of the user

I
Igor Vorotnev, 2014-05-14
@HeadOnFire

Until recently, the VK API did not issue soap for external requests. But it seems that with the final departure of Durov, the situation begins to change. Literally today, I announced to the client that it was impossible to receive an email, and he showed me a screenshot taken on April 24 of this year, on which the pop-up window just gave access to the email address. That is, the fact - now it is possible. How - I'm still digging myself. I didn't find it in the docks. I subscribe to the topic, maybe someone will find a solution earlier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question