K
K
Konstantin Chuykov2015-05-18 17:03:58
In contact with
Konstantin Chuykov, 2015-05-18 17:03:58

How to get email via VK PassportJS API?

Hello! I'm trying to get the user's E-mail through Vkontakte authorization using passport-vkontake, but this module does not provide for receiving emails, or rather scope=email, we can send, but VK email comes with a token (that is, on the first request), and not when requesting to retrieve user data. I tried to find out where the response to receiving a token is stored in the module code, but alas, it’s already 2 am, and I don’t know where to dig anymore. I have already reached the node_modules ▸ passport-vkontakte ▸ node_modules ▸ passport-oauth2 ▸ node_modules ▸ oauth ▸ lib /oauth2.js file, and I understand that the answer is somewhere here:

this._request("POST", this._getAccessTokenUrl(), post_headers, post_data, null, function(error, data, response) {
    if( error )  callback(error);
    else {
      var results;
      try {
        // As of http://tools.ietf.org/html/draft-ietf-oauth-v2-07
        // responses should be in JSON
        results= JSON.parse( data );
      }
      catch(e) {
        // .... However both Facebook + Github currently use rev05 of the spec
        // and neither seem to specify a content-type correctly in their response headers :(
        // clients of these services will suffer a *minor* performance cost of the exception
        // being thrown
        results= querystring.parse( data );
      }
      var access_token= results["access_token"];
      var refresh_token= results["refresh_token"];
      delete results["refresh_token"];
      callback(null, access_token, refresh_token, results); // callback results =-=
    }
  });
}

I understand that the answer is stored here var access_token= results["access_token"]; you just need to remove ["access_token"]. But not a fact.
But I don't know how to get it out. So far it looks like this:
passport.use('vk', new vk({
    clientID: config.get('auth:vk:app_id'),
    clientSecret: config.get('auth:vk:secret'),
    callbackURL: config.get('app:url') + "/auth/vk/callback",
},function (accessToken, refreshToken, profile, done) {
    console.log(accessToken);
}));

We need your expert help. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Chuykov, 2015-05-19
@chuikoffru

All problem solved!!! Whooooooo!!!

passport.use('vk', new vk({
    clientID: config.get('auth:vk:app_id'),
    clientSecret: config.get('auth:vk:secret'),
    callbackURL: config.get('app:url') + "/auth/vk/callback",
    apiVersion : "5.32"
},function (accessToken, refreshToken, params, profile, done) {
    console.log(params);
}));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question