|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.PinterestApi; |
| 4 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +import com.github.scribejava.core.model.OAuthRequest; |
| 6 | +import com.github.scribejava.core.model.Response; |
| 7 | +import com.github.scribejava.core.model.Token; |
| 8 | +import com.github.scribejava.core.model.Verb; |
| 9 | +import com.github.scribejava.core.model.Verifier; |
| 10 | +import com.github.scribejava.core.oauth.OAuthService; |
| 11 | + |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +public class PinterestExample { |
| 15 | + |
| 16 | + private static final String PROTECTED_RESOURCE_URL = "https://api.pinterest.com/v1/me/?access_token?access_token="; |
| 17 | + private static final Token EMPTY_TOKEN = null; |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + // Replace these with your own api key and secret |
| 21 | + String apiKey = "your_app_id"; |
| 22 | + String apiSecret = "your_app_secret"; |
| 23 | + OAuthService service = new ServiceBuilder() |
| 24 | + .provider(PinterestApi.class) |
| 25 | + .apiKey(apiKey) |
| 26 | + .apiSecret(apiSecret) |
| 27 | + .scope("read_public,write_public,read_relationships,write_relationships") |
| 28 | + .callback("https://localhost:9000/") // Add as valid callback in developer portal |
| 29 | + .build(); |
| 30 | + Scanner in = new Scanner(System.in); |
| 31 | + |
| 32 | + System.out.println("=== Pinterest's OAuth Workflow ==="); |
| 33 | + System.out.println(); |
| 34 | + |
| 35 | + // Obtain the Authorization URL |
| 36 | + System.out.println("Fetching the Authorization URL..."); |
| 37 | + String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN); |
| 38 | + System.out.println("Got the Authorization URL!"); |
| 39 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 40 | + System.out.println(authorizationUrl); |
| 41 | + System.out.println("And paste the authorization code here"); |
| 42 | + System.out.print(">>"); |
| 43 | + Verifier verifier = new Verifier(in.nextLine()); |
| 44 | + System.out.println(); |
| 45 | + |
| 46 | + // Trade the Request Token and Verfier for the Access Token |
| 47 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 48 | + Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier); |
| 49 | + System.out.println("Got the Access Token!"); |
| 50 | + System.out.println("(if your curious it looks like this: " + accessToken + " )"); |
| 51 | + System.out.println(); |
| 52 | + |
| 53 | + // Now let's go and ask for a protected resource! |
| 54 | + System.out.println("Now we're going to access a protected resource..."); |
| 55 | + OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL + accessToken.getToken(), service); |
| 56 | + service.signRequest(accessToken, request); |
| 57 | + Response response = request.send(); |
| 58 | + System.out.println("Got it! Lets see what we found..."); |
| 59 | + System.out.println(); |
| 60 | + System.out.println(response.getCode()); |
| 61 | + System.out.println(response.getBody()); |
| 62 | + |
| 63 | + System.out.println(); |
| 64 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 65 | + |
| 66 | + } |
| 67 | +} |
0 commit comments