Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8afb142

Browse files
TomTaschefernandezpablo85
authored andcommitted
Add Foursquare2 API and example
1 parent bfcf065 commit 8afb142

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
4141
* FEATURE: Added rawResponse to Token, for extracting additional parameters (thanks Dirk McCormick)
4242
* FEATURE: Added Dropbox Api
4343
* FIX: Token responses now work despite of the token/secret order
44+
* FEATURE: Added Foursquare OAuth 2.0 Api (thanks Tom Tasche)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.*;
4+
import org.scribe.model.*;
5+
import org.scribe.utils.*;
6+
7+
public class Foursquare2Api extends DefaultApi20
8+
{
9+
private static final String AUTHORIZATION_URL = "https://foursquare.com/oauth2/authenticate?client_id=%s&response_type=code&redirect_uri=%s";
10+
11+
@Override
12+
public String getAccessTokenEndpoint()
13+
{
14+
return "https://foursquare.com/oauth2/access_token?grant_type=authorization_code";
15+
}
16+
17+
@Override
18+
public String getAuthorizationUrl(OAuthConfig config)
19+
{
20+
Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. Foursquare2 does not support OOB");
21+
return String.format(AUTHORIZATION_URL, config.getApiKey(), URLUtils.formURLEncode(config.getCallback()));
22+
}
23+
24+
@Override
25+
public AccessTokenExtractor getAccessTokenExtractor()
26+
{
27+
return new JsonTokenExtractor();
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.scribe.extractors;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
import org.scribe.exceptions.OAuthException;
7+
import org.scribe.extractors.TokenExtractor20Impl;
8+
import org.scribe.model.Token;
9+
import org.scribe.utils.Preconditions;
10+
import org.scribe.utils.URLUtils;
11+
12+
public class FoursquareAccessTokenExtractor extends TokenExtractor20Impl {
13+
14+
private static final String TOKEN_REGEX = ".[\\s]+\"access_token\":\"(\\S*?)(&(\\S*))?\"[\\s]+.";
15+
private static final String EMPTY_SECRET = "";
16+
17+
public Token extract(String response) {
18+
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
19+
20+
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
21+
if (matcher.matches()) {
22+
String token = URLUtils.formURLDecode(matcher.group(1));
23+
return new Token(token, EMPTY_SECRET);
24+
} else {
25+
throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.scribe.extractors;
2+
3+
import java.util.regex.*;
4+
5+
import org.scribe.exceptions.*;
6+
import org.scribe.model.*;
7+
import org.scribe.utils.*;
8+
9+
public class JsonTokenExtractor implements AccessTokenExtractor
10+
{
11+
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\"(\\S*)\"");
12+
13+
@Override
14+
public Token extract(String response)
15+
{
16+
Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
17+
Matcher matcher = accessTokenPattern.matcher(response);
18+
if(matcher.find())
19+
{
20+
return new Token(matcher.group(1), "");
21+
}
22+
else
23+
{
24+
throw new OAuthException("Cannot extract an acces token. Response was: "+response);
25+
}
26+
}
27+
28+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.scribe.examples;
2+
3+
import java.util.*;
4+
5+
import org.scribe.builder.*;
6+
import org.scribe.builder.api.*;
7+
import org.scribe.model.*;
8+
import org.scribe.oauth.*;
9+
10+
public class Foursquare2Example
11+
{
12+
private static final String PROTECTED_RESOURCE_URL = "https://api.foursquare.com/v2/users/self/friends?oauth_token=";
13+
private static final Token EMPTY_TOKEN = null;
14+
15+
public static void main(String[] args)
16+
{
17+
// Replace these with your own api key and secret
18+
String apiKey = "FEGFXJUFANVVDHVSNUAMUKTTXCP1AJQD53E33XKJ44YP1S4I";
19+
String apiSecret = "AYWKUL5SWPNC0CTQ202QXRUG2NLZYXMRA34ZSDW4AUYBG2RC";
20+
OAuthService service = new ServiceBuilder()
21+
.provider(Foursquare2Api.class)
22+
.apiKey(apiKey)
23+
.apiSecret(apiSecret)
24+
.callback("http://localhost:9000/")
25+
.build();
26+
Scanner in = new Scanner(System.in);
27+
28+
System.out.println("=== Foursquare2's OAuth Workflow ===");
29+
System.out.println();
30+
31+
// Obtain the Authorization URL
32+
System.out.println("Fetching the Authorization URL...");
33+
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
34+
System.out.println("Got the Authorization URL!");
35+
System.out.println("Now go and authorize Scribe here:");
36+
System.out.println(authorizationUrl);
37+
System.out.println("And paste the authorization code here");
38+
System.out.print(">>");
39+
Verifier verifier = new Verifier(in.nextLine());
40+
System.out.println();
41+
42+
// Trade the Request Token and Verfier for the Access Token
43+
System.out.println("Trading the Request Token for an Access Token...");
44+
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
45+
System.out.println("Got the Access Token!");
46+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
47+
System.out.println();
48+
49+
// Now let's go and ask for a protected resource!
50+
System.out.println("Now we're going to access a protected resource...");
51+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL + accessToken.getToken());
52+
service.signRequest(accessToken, request);
53+
Response response = request.send();
54+
System.out.println("Got it! Lets see what we found...");
55+
System.out.println();
56+
System.out.println(response.getCode());
57+
System.out.println(response.getBody());
58+
59+
System.out.println();
60+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
61+
62+
}
63+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.scribe.extractors;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.*;
6+
import org.scribe.model.*;
7+
8+
public class JsonTokenExtractorTest
9+
{
10+
private String response = "'{ \"access_token\":\"I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X\"}'";
11+
private JsonTokenExtractor extractor = new JsonTokenExtractor();
12+
13+
@Test
14+
public void shouldParseResponse()
15+
{
16+
Token token = extractor.extract(response);
17+
assertEquals(token.getToken(), "I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X");
18+
}
19+
20+
@Test(expected=IllegalArgumentException.class)
21+
public void shouldThrowExceptionIfForNullParameters()
22+
{
23+
extractor.extract(null);
24+
}
25+
26+
@Test(expected=IllegalArgumentException.class)
27+
public void shouldThrowExceptionIfForEmptyStrings()
28+
{
29+
extractor.extract("");
30+
}
31+
}

0 commit comments

Comments
 (0)