Skip to content

Commit 71f8539

Browse files
committed
add email field to VKOAuth2AccessToken (thanks to https://github.com/grouzen)
1 parent 787668d commit 71f8539

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

changelog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
2-
* fix missing support for scope for refresh_token grant_type [thanks to https://github.com/tlxtellef]
2+
* fix missing support for scope for refresh_token grant_type (thanks to https://github.com/tlxtellef)
3+
* add email field to VKOAuth2AccessToken (thanks to https://github.com/grouzen)
34

45
[5.3.0]
56
* fix Salesforce API (thanks to https://github.com/jhorowitz-firedrum)

scribejava-apis/src/main/java/com/github/scribejava/apis/VkontakteApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.github.scribejava.apis;
22

3+
import com.github.scribejava.apis.vk.VKJsonTokenExtractor;
34
import com.github.scribejava.core.builder.api.ClientAuthenticationType;
45
import com.github.scribejava.core.builder.api.DefaultApi20;
56
import com.github.scribejava.core.builder.api.OAuth2SignatureType;
7+
import com.github.scribejava.core.extractors.TokenExtractor;
8+
import com.github.scribejava.core.model.OAuth2AccessToken;
69
import com.github.scribejava.core.model.Verb;
710

811
public class VkontakteApi extends DefaultApi20 {
@@ -13,6 +16,7 @@ protected VkontakteApi() {
1316
}
1417

1518
private static class InstanceHolder {
19+
1620
private static final VkontakteApi INSTANCE = new VkontakteApi();
1721
}
1822

@@ -44,4 +48,9 @@ public OAuth2SignatureType getSignatureType() {
4448
public ClientAuthenticationType getClientAuthenticationType() {
4549
return ClientAuthenticationType.REQUEST_BODY;
4650
}
51+
52+
@Override
53+
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
54+
return VKJsonTokenExtractor.instance();
55+
}
4756
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.scribejava.apis.vk;
2+
3+
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* additionally parses email
8+
*/
9+
public class VKJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
10+
11+
private static final Pattern EMAIL_REGEX_PATTERN = Pattern.compile("\"email\"\\s*:\\s*\"(\\S*?)\"");
12+
13+
protected VKJsonTokenExtractor() {
14+
}
15+
16+
private static class InstanceHolder {
17+
18+
private static final VKJsonTokenExtractor INSTANCE = new VKJsonTokenExtractor();
19+
}
20+
21+
public static VKJsonTokenExtractor instance() {
22+
return InstanceHolder.INSTANCE;
23+
}
24+
25+
@Override
26+
protected VKOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
27+
String refreshToken, String scope, String response) {
28+
return new VKOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
29+
extractParameter(response, EMAIL_REGEX_PATTERN, false), response);
30+
}
31+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.scribejava.apis.vk;
2+
3+
import com.github.scribejava.core.model.OAuth2AccessToken;
4+
import java.util.Objects;
5+
6+
public class VKOAuth2AccessToken extends OAuth2AccessToken {
7+
8+
private static final long serialVersionUID = -3539517142527580499L;
9+
10+
private final String email;
11+
12+
public VKOAuth2AccessToken(String accessToken, String email, String rawResponse) {
13+
this(accessToken, null, null, null, null, email, rawResponse);
14+
}
15+
16+
public VKOAuth2AccessToken(String accessToken, String tokenType, Integer expiresIn, String refreshToken,
17+
String scope, String email, String rawResponse) {
18+
super(accessToken, tokenType, expiresIn, refreshToken, scope, rawResponse);
19+
this.email = email;
20+
}
21+
22+
public String getEmail() {
23+
return email;
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
int hash = super.hashCode();
29+
hash = 37 * hash + Objects.hashCode(email);
30+
return hash;
31+
}
32+
33+
@Override
34+
public boolean equals(Object obj) {
35+
if (this == obj) {
36+
return true;
37+
}
38+
if (obj == null) {
39+
return false;
40+
}
41+
if (getClass() != obj.getClass()) {
42+
return false;
43+
}
44+
if (!super.equals(obj)) {
45+
return false;
46+
}
47+
48+
return Objects.equals(email, ((VKOAuth2AccessToken) obj).getEmail());
49+
}
50+
}

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/VkontakteExample.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Scanner;
44
import com.github.scribejava.core.builder.ServiceBuilder;
55
import com.github.scribejava.apis.VkontakteApi;
6+
import com.github.scribejava.apis.vk.VKOAuth2AccessToken;
67
import com.github.scribejava.core.model.OAuth2AccessToken;
78
import com.github.scribejava.core.model.OAuthRequest;
89
import com.github.scribejava.core.model.Response;
@@ -26,7 +27,7 @@ public static void main(String... args) throws IOException, InterruptedException
2627
final String clientSecret = "your client secret";
2728
final OAuth20Service service = new ServiceBuilder(clientId)
2829
.apiSecret(clientSecret)
29-
.scope("wall,offline") // replace with desired scope
30+
.scope("wall,offline,email") // replace with desired scope
3031
.callback("http://your.site.com/callback")
3132
.build(VkontakteApi.instance());
3233
final Scanner in = new Scanner(System.in);
@@ -50,6 +51,10 @@ public static void main(String... args) throws IOException, InterruptedException
5051
final OAuth2AccessToken accessToken = service.getAccessToken(code);
5152
System.out.println("Got the Access Token!");
5253
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
54+
if (accessToken instanceof VKOAuth2AccessToken) {
55+
System.out.println("it's a VKOAuth2AccessToken, it has email field = '"
56+
+ ((VKOAuth2AccessToken) accessToken).getEmail() + "'.");
57+
}
5358
System.out.println();
5459

5560
// Now let's go and ask for a protected resource!

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuth2AccessToken.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public OAuth2AccessToken(String accessToken, String tokenType, Integer expiresIn
7373
this.scope = scope;
7474
}
7575

76-
7776
public String getAccessToken() {
7877
return accessToken;
7978
}

0 commit comments

Comments
 (0)