|
| 1 | +packagecom.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +importcom.github.scribejava.apis.PinterestApi; |
| 4 | +importcom.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +importcom.github.scribejava.core.model.OAuthRequest; |
| 6 | +importcom.github.scribejava.core.model.Response; |
| 7 | +importcom.github.scribejava.core.model.Token; |
| 8 | +importcom.github.scribejava.core.model.Verb; |
| 9 | +importcom.github.scribejava.core.model.Verifier; |
| 10 | +importcom.github.scribejava.core.oauth.OAuthService; |
| 11 | + |
| 12 | +importjava.util.Scanner; |
| 13 | + |
| 14 | +publicclassPinterestExample { |
| 15 | + |
| 16 | +privatestaticfinalStringPROTECTED_RESOURCE_URL ="https://api.pinterest.com/v1/me/?access_token?access_token="; |
| 17 | +privatestaticfinalTokenEMPTY_TOKEN =null; |
| 18 | + |
| 19 | +publicstaticvoidmain(String[]args) { |
| 20 | +// Replace these with your own api key and secret |
| 21 | +StringapiKey ="your_app_id"; |
| 22 | +StringapiSecret ="your_app_secret"; |
| 23 | +OAuthServiceservice =newServiceBuilder() |
| 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 | +Scannerin =newScanner(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 | +StringauthorizationUrl =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 | +Verifierverifier =newVerifier(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 | +TokenaccessToken =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 | +OAuthRequestrequest =newOAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL +accessToken.getToken(),service); |
| 56 | +service.signRequest(accessToken,request); |
| 57 | +Responseresponse =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 | +} |