Skip to content

Commit 45a0f96

Browse files
Added Foursquare example
1 parent 928a32d commit 45a0f96

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

README.textile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ h3. Supports all major OAuth APIs out-of-the-box
3030

3131
* Twitter
3232

33-
* Salesforce
34-
3533
* Foursquare
3634

3735
* and many more! check the "examples folder":http://github.com/fernandezpablo85/scribe-java/tree/master/src/test/java/org/scribe/examples

src/main/java/org/scribe/builder/api/FoursquareApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ public class FoursquareApi extends DefaultApi10a
55
@Override
66
public String getAccessTokenEndpoint()
77
{
8-
throw new UnsupportedOperationException();
8+
return "http://foursquare.com/oauth/access_token";
99
}
1010

1111
@Override
1212
public String getRequestTokenEndpoint()
1313
{
14-
throw new UnsupportedOperationException();
14+
return "http://foursquare.com/oauth/request_token";
1515
}
1616
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.scribe.examples;
2+
3+
import java.util.Scanner;
4+
5+
import org.scribe.builder.*;
6+
import org.scribe.builder.api.*;
7+
import org.scribe.model.*;
8+
import org.scribe.model.Request.*;
9+
import org.scribe.oauth.*;
10+
11+
public class FoursquareExample
12+
{
13+
private static final String AUTHORIZE_URL = "http://foursquare.com/oauth/authorize?oauth_token=";
14+
private static final String PROTECTED_RESOURCE_URL = "http://api.foursquare.com/v1/user";
15+
16+
public static void main(String[] args)
17+
{
18+
OAuthService service = new ServiceBuilder()
19+
.provider(FoursquareApi.class)
20+
.apiKey("FEGFXJUFANVVDHVSNUAMUKTTXCP1AJQD53E33XKJ44YP1S4I")
21+
.apiSecret("AYWKUL5SWPNC0CTQ202QXRUG2NLZYXMRA34ZSDW4AUYBG2RC")
22+
.build();
23+
Scanner in = new Scanner(System.in);
24+
25+
System.out.println("=== Foursquare's OAuth Workflow ===");
26+
System.out.println();
27+
28+
// Obtain the Request Token
29+
System.out.println("Fetching the Request Token...");
30+
Token requestToken = service.getRequestToken();
31+
System.out.println("Got the Request Token!");
32+
System.out.println();
33+
34+
System.out.println("Now go and authorize Scribe here:");
35+
System.out.println(AUTHORIZE_URL + requestToken.getToken());
36+
System.out.println("And paste the verifier here");
37+
System.out.print(">>");
38+
Verifier verifier = new Verifier(in.nextLine());
39+
System.out.println();
40+
41+
// Trade the Request Token and Verfier for the Access Token
42+
System.out.println("Trading the Request Token for an Access Token...");
43+
Token accessToken = service.getAccessToken(requestToken, verifier);
44+
System.out.println("Got the Access Token!");
45+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
46+
System.out.println();
47+
48+
// Now let's go and ask for a protected resource!
49+
System.out.println("Now we're going to access a protected resource...");
50+
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
51+
service.signRequest(accessToken, request);
52+
Response response = request.send();
53+
System.out.println("Got it! Lets see what we found...");
54+
System.out.println();
55+
System.out.println(response.getBody());
56+
57+
System.out.println();
58+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
59+
}
60+
61+
}

0 commit comments

Comments
 (0)