Skip to content

Commit ca79500

Browse files
Borisfernandezpablo85
authored andcommitted
Implemented Vkontakte.ru support
1 parent 543b534 commit ca79500

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353

5454
[1.2.1]
5555
* FEATURE: Added custom charset support to Request (thanks Eric Genet)
56+
* FEATURE: Added support for Vkontakte (thanks dotbg)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.*;
4+
import org.scribe.utils.*;
5+
import org.scribe.model.*;
6+
7+
/**
8+
* @author Boris G. Tsirkin <mail@dotbg.name>
9+
* @since 20.4.2011
10+
*/
11+
public class VkontakteApi extends DefaultApi20
12+
{
13+
private static final String AUTHORIZE_URL = "https://api.vkontakte.ru/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code";
14+
private static final String SCOPED_AUTHORIZE_URL = String.format("%s&scope=%%s", AUTHORIZE_URL);
15+
16+
@Override
17+
public String getAccessTokenEndpoint()
18+
{
19+
return "https://api.vkontakte.ru/oauth/access_token";
20+
}
21+
22+
@Override
23+
public String getAuthorizationUrl(OAuthConfig config)
24+
{
25+
Preconditions.checkValidUrl(config.getCallback(), "Valid url is required for a callback. Vkontakte does not support OOB");
26+
if(config.hasScope())// Appending scope if present
27+
{
28+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()),URLUtils.formURLEncode(config.getScope()));
29+
}
30+
else
31+
{
32+
return String.format(AUTHORIZE_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
33+
}
34+
}
35+
36+
@Override
37+
public AccessTokenExtractor getAccessTokenExtractor()
38+
{
39+
return new JsonTokenExtractor();
40+
}
41+
}

src/main/java/org/scribe/extractors/JsonTokenExtractor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class JsonTokenExtractor implements AccessTokenExtractor
1010
{
11-
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*)\"");
11+
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*?)\"");
1212

1313
@Override
1414
public Token extract(String response)
@@ -17,11 +17,11 @@ public Token extract(String response)
1717
Matcher matcher = accessTokenPattern.matcher(response);
1818
if(matcher.find())
1919
{
20-
return new Token(matcher.group(1), "");
20+
return new Token(matcher.group(1), "", response);
2121
}
2222
else
2323
{
24-
throw new OAuthException("Cannot extract an acces token. Response was: "+response);
24+
throw new OAuthException("Cannot extract an acces token. Response was: " + response);
2525
}
2626
}
2727

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.scribe.examples;
2+
3+
import org.scribe.builder.*;
4+
import org.scribe.builder.api.*;
5+
import org.scribe.model.*;
6+
import org.scribe.oauth.*;
7+
8+
import java.util.*;
9+
10+
/**
11+
* @author Boris G. Tsirkin <mail@dotbg.name>
12+
* @since 20.4.2011
13+
*/
14+
public class VkontakteExample
15+
{
16+
private static final String NETWORK_NAME = "Vkontakte.ru";
17+
private static final String PROTECTED_RESOURCE_URL = "https://api.vkontakte.ru/method/friends.get";
18+
private static final Token EMPTY_TOKEN = null;
19+
20+
public static void main(String[] args)
21+
{
22+
// Replace these with your own api key and secret
23+
final String clientId = "your app id";
24+
final String apiSecret = "your api secret";
25+
OAuthService service = new ServiceBuilder()
26+
.provider(VkontakteApi.class)
27+
.apiKey(clientId)
28+
.apiSecret(apiSecret)
29+
.scope("friends,wall,offline") // replace with desired scope
30+
.callback("http://your.site.com/callback")
31+
.build();
32+
Scanner in = new Scanner(System.in);
33+
34+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
35+
System.out.println();
36+
37+
// Obtain the Authorization URL
38+
System.out.println("Fetching the Authorization URL...");
39+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
40+
System.out.println("Got the Authorization URL!");
41+
System.out.println("Now go and authorize Scribe here:");
42+
System.out.println(authorizationUrl);
43+
System.out.println("And paste the authorization code here");
44+
System.out.print(">>");
45+
Verifier verifier = new Verifier(in.nextLine());
46+
System.out.println();
47+
48+
// Trade the Request Token and Verfier for the Access Token
49+
System.out.println("Trading the Request Token for an Access Token...");
50+
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
51+
System.out.println("Got the Access Token!");
52+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
53+
System.out.println();
54+
55+
// Now let's go and ask for a protected resource!
56+
System.out.println("Now we're going to access a protected resource...");
57+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
58+
service.signRequest(accessToken, request);
59+
Response response = request.send();
60+
System.out.println("Got it! Lets see what we found...");
61+
System.out.println();
62+
System.out.println(response.getCode());
63+
System.out.println(response.getBody());
64+
65+
System.out.println();
66+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
67+
}
68+
}

0 commit comments

Comments
 (0)