All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pac4j.oauth.client.VkClient Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
package org.pac4j.oauth.client;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.github.scribejava.apis.VkontakteApi;
import com.github.scribejava.core.builder.api.BaseApi;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.vk.VkProfile;

/**
 * 

This class is the OAuth client to authenticate users in Vk.

*

The scope can be defined to require specific permissions from the user * by using the {@link #setScope(String)} method. By default, the scope * is : PERMISSIONS.

*

It returns a {@link org.pac4j.oauth.profile.vk.VkProfile}.

*

More information at https://vk.com/dev/users.get

* * @author indvdum (gotoindvdum[at]gmail[dot]com) * @since 1.5 * */ public class VkClient extends BaseOAuth20Client { public final static String DEFAULT_FIELDS = "sex,bdate,photo_50,photo_100,photo_200_orig,photo_200,photo_400_orig,photo_max,photo_max_orig,online,online_mobile,lists,domain,has_mobile,contacts,connections,site,education,can_post,can_see_all_posts,can_see_audio,can_write_private_message,status,common_count,relation,relatives"; protected String fields = DEFAULT_FIELDS; public final static String DEFAULT_SCOPE = "PERMISSIONS"; protected String scope = DEFAULT_SCOPE; protected final static String BASE_URL = "https://api.vk.com/method/users.get"; public VkClient() { } public VkClient(final String key, final String secret) { setKey(key); setSecret(secret); } @Override protected BaseApi getApi() { return VkontakteApi.instance(); } @Override protected String getOAuthScope() { return this.scope; } @Override protected String getProfileUrl(final OAuth2AccessToken accessToken) { String url = BASE_URL + "?fields=" + this.fields; return url; } @Override protected VkProfile extractUserProfile(final String body) throws HttpAction { final VkProfile profile = new VkProfile(); JsonNode json = JsonHelper.getFirstNode(body); if (json != null) { ArrayNode array = (ArrayNode) json.get("response"); JsonNode userNode = array.get(0); profile.setId(JsonHelper.getElement(userNode, "uid")); for (final String attribute : profile.getAttributesDefinition().getPrimaryAttributes()) { profile.addAttribute(attribute, JsonHelper.getElement(userNode, attribute)); } } return profile; } public String getScope() { return this.scope; } public void setScope(final String scope) { this.scope = scope; } public String getFields() { return fields; } public void setFields(String fields) { this.fields = fields; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy