|
| 1 | +package org.scribe.examples; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +import org.scribe.builder.ServiceBuilder; |
| 6 | +import org.scribe.builder.api.FreelancerAPI; |
| 7 | +import org.scribe.model.OAuthRequest; |
| 8 | +import org.scribe.model.Response; |
| 9 | +import org.scribe.model.SignatureType; |
| 10 | +import org.scribe.model.Token; |
| 11 | +import org.scribe.model.Verb; |
| 12 | +import org.scribe.model.Verifier; |
| 13 | +import org.scribe.oauth.OAuthService; |
| 14 | + |
| 15 | +public class FreelancerExample |
| 16 | +{ |
| 17 | + |
| 18 | + private static final String NETWORK_NAME = "Freelancer"; |
| 19 | + private static final String AUTHORIZE_URL = "http://www.sandbox.freelancer.com/users/api-token/auth.php?oauth_token="; |
| 20 | + private static final String PROTECTED_RESOURCE_URL = "http://api.sandbox.freelancer.com/Job/getJobList.json"; |
| 21 | + private static final String SCOPE = "http://api.sandbox.freelancer.com"; |
| 22 | + |
| 23 | + public static void main(String[] args) |
| 24 | + { |
| 25 | + OAuthService service = new ServiceBuilder() |
| 26 | + .provider(FreelancerAPI.class) |
| 27 | + .signatureType(SignatureType.QueryString) |
| 28 | + .apiKey("7f5a168a0bfdbd15b4a9ea2a969661c731cdea56") |
| 29 | + .apiSecret("7bb8961b94873802f1c5344f671a518e087f5785").scope(SCOPE) |
| 30 | + .build(); |
| 31 | + Scanner in = new Scanner(System.in); |
| 32 | + |
| 33 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); |
| 34 | + System.out.println(); |
| 35 | + |
| 36 | + // Obtain the Request Token |
| 37 | + System.out.println("Fetching the Request Token..."); |
| 38 | + Token requestToken = service.getRequestToken(); |
| 39 | + System.out.println("Got the Request Token!"); |
| 40 | + System.out.println("(if your curious it looks like this: " + requestToken |
| 41 | + + " )"); |
| 42 | + System.out.println(); |
| 43 | + |
| 44 | + System.out.println("Now go and authorize Scribe here:"); |
| 45 | + System.out.println(AUTHORIZE_URL + requestToken.getToken()); |
| 46 | + System.out.println("And paste the verifier here"); |
| 47 | + System.out.print(">>"); |
| 48 | + Verifier verifier = new Verifier(in.nextLine()); |
| 49 | + System.out.println(); |
| 50 | + |
| 51 | + // Trade the Request Token and Verfier for the Access Token |
| 52 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 53 | + Token accessToken = service.getAccessToken(requestToken, verifier); |
| 54 | + System.out.println("Got the Access Token!"); |
| 55 | + System.out.println("(if your curious it looks like this: " + accessToken |
| 56 | + + " )"); |
| 57 | + System.out.println(); |
| 58 | + |
| 59 | + // Now let's go and ask for a protected resource! |
| 60 | + System.out.println("Now we're going to access a protected resource..."); |
| 61 | + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 62 | + service.signRequest(accessToken, request); |
| 63 | + request.addHeader("GData-Version", "3.0"); |
| 64 | + Response response = request.send(); |
| 65 | + System.out.println("Got it! Lets see what we found..."); |
| 66 | + System.out.println(); |
| 67 | + System.out.println(response.getCode()); |
| 68 | + System.out.println(response.getBody()); |
| 69 | + |
| 70 | + System.out.println(); |
| 71 | + System.out |
| 72 | + .println("Thats it man! Go and build something awesome with Scribe! :)"); |
| 73 | + } |
| 74 | +} |
0 commit comments