Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User info from VKontakte in Android

Tags:

android

vk

I am writing an application that needs to use the user data taken from the client social network VKontakte. I did authorize VKontakte.

VKSdk.initialize(sdkListener, String.valueOf(idVK), VKAccessToken.tokenFromSharedPreferences(this, sTokenKey));

And got AccessToken. As now I get the name and email user?

like image 642
Alex Sh Avatar asked Feb 14 '23 14:02

Alex Sh


1 Answers

You can request email scope from user, then get email from access token:

String email = VKSdk.getAccessToken().email;
String userId = VKSdk.getAccessToken().userId;

//Get user info
VKApi.users().get().executeWithListener(new VKRequest.VKRequestListener() {
    @Override
    public void onComplete(VKResponse response) {
        VKApiUser user = ((VKList<VKApiUser>)response.parsedModel).get(0);
        Log.d("User name", user.first_name + " " + user.last_name);
    }
});

But remember, email is only available after first access request.

Old answer:

You can't get an email. This is not available. But you can get user name:

VKApi.users().get().executeWithListener(new VKRequest.VKRequestListener() {
    @Override
    public void onComplete(VKResponse response) {
        VKApiUser user = ((VKList<VKApiUser>)response.parsedModel).get(0);
        Log.d("User name", user.first_name + " " + user.last_name);
    }
});

like image 113
Roman Truba Avatar answered Mar 08 '23 15:03

Roman Truba