2

I'm trying to implement Oauth2 authorization using Rauth for vk.com provider and I have the following issue:

As far as I know there is no way to obtain users email address through vk.com api call, but it sends email address with access_token in json format.

My problem is: I do not know how to obtain it from Rauth's "session" object, there is an access_token field but no way to get an email address.

Here is the code:

def callback(self):
    def decode_json(payload):
        return json.loads(payload.decode('utf-8'))
    if 'code' not in request.args:
        return None, None, None
    oauth_session = self.service.get_auth_session(
        data={'code': request.args['code'],
              'grant_type': 'authorization_code',
              'redirect_uri': self.get_callback_url()},
        decoder=decode_json
    )
    me = oauth_session.get("some call to vk.api").json()

Thank you for your help!

Roman Godov
  • 23
  • 1
  • 5

1 Answers1

2

Let me explain. Actually, email is returned by Vkontakte along with access token in case it is provided.

You don't need to use oauth_session.get since it does make a new request, but it is already done, when you got access token. Try to get an object property with oauth_session.email.

P.S. you can find email using get_raw_access_token.

shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • Thank you for your answer but as I have mentioned in my question there is no such property in oauth_session object as 'email', there is 'access_token', Rauth extracts it from vkontakte response but not email – Roman Godov Aug 20 '17 at 16:35
  • Sounds strange. But it is returned exactly with access_token. Ok, good luck. I'd check `get_raw_access_token` if email exists in raw return. This is up to you then. – shukshin.ivan Aug 20 '17 at 16:49
  • You are correct Ivan `get_raw_access_token` helps, there is `.json` method which returns raw json sent by vkontakte containing email address. Thanks a lot! – Roman Godov Aug 20 '17 at 20:01
  • Glad to hear that. I knew the truth was somewhere before making a new request) – shukshin.ivan Aug 20 '17 at 20:02
  • You can check the answer as helpful. Thanks – shukshin.ivan Aug 20 '17 at 20:03
  • sorry, I guess I don't have enough reputation for my vote to display. I marked answer as accepted but may be it makes sense to mention `get_raw_access_token` there. – Roman Godov Aug 20 '17 at 20:16
  • OK, I've just added. But nevertheless it exists here. – shukshin.ivan Aug 20 '17 at 20:30