|
| 1 | +packagecom.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +importcom.github.scribejava.apis.KakaoApi; |
| 4 | +importcom.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +importcom.github.scribejava.core.model.OAuth2AccessToken; |
| 6 | +importcom.github.scribejava.core.model.OAuthRequest; |
| 7 | +importcom.github.scribejava.core.model.Response; |
| 8 | +importcom.github.scribejava.core.model.Verb; |
| 9 | +importcom.github.scribejava.core.oauth.OAuth20Service; |
| 10 | + |
| 11 | +importjava.io.IOException; |
| 12 | +importjava.util.Scanner; |
| 13 | +importjava.util.concurrent.ExecutionException; |
| 14 | + |
| 15 | +publicclassKakaoExample { |
| 16 | + |
| 17 | +privatestaticfinalStringNETWORK_NAME ="Kakao"; |
| 18 | +privatestaticfinalStringPROTECTED_RESOURCE_URL ="https://kapi.kakao.com/v2/user/me"; |
| 19 | + |
| 20 | +privateKakaoExample() { |
| 21 | + } |
| 22 | + |
| 23 | +@SuppressWarnings("PMD.SystemPrintln") |
| 24 | +publicstaticvoidmain(String...args)throwsIOException,InterruptedException,ExecutionException { |
| 25 | +// Replace these with your client id and secret |
| 26 | +finalStringclientId ="your client id"; |
| 27 | + |
| 28 | +finalOAuth20Serviceservice =newServiceBuilder(clientId) |
| 29 | + .callback("http://www.example.com/oauth_callback/") |
| 30 | + .build(KakaoApi.instance()); |
| 31 | + |
| 32 | +finalScannerin =newScanner(System.in,"UTF-8"); |
| 33 | + |
| 34 | +System.out.println("=== " +NETWORK_NAME +"'s OAuth Workflow ==="); |
| 35 | +System.out.println(); |
| 36 | + |
| 37 | +// Obtain the Authorization URL |
| 38 | +System.out.println("Fetching the Authorization URL..."); |
| 39 | +finalStringauthorizationUrl =service.getAuthorizationUrl(); |
| 40 | +System.out.println("Got the Authorization URL!"); |
| 41 | +System.out.println("Now go and authorize ScribeJava here:"); |
| 42 | +System.out.println(authorizationUrl); |
| 43 | +System.out.println("And paste the authorization code here"); |
| 44 | +System.out.print(">>"); |
| 45 | +finalStringcode =in.nextLine(); |
| 46 | +System.out.println(); |
| 47 | + |
| 48 | +System.out.println("Trading the Authorization Code for an Access Token..."); |
| 49 | +finalOAuth2AccessTokenaccessToken =service.getAccessToken(code); |
| 50 | + |
| 51 | +System.out.println("Got the Access Token!"); |
| 52 | +System.out.println("(The raw response looks like this: " +accessToken.getRawResponse() +"')"); |
| 53 | +System.out.println(); |
| 54 | + |
| 55 | +// Now let's go and ask for a protected resource! |
| 56 | +System.out.println("Now we're going to access a protected resource..."); |
| 57 | +finalOAuthRequestrequest =newOAuthRequest(Verb.GET,PROTECTED_RESOURCE_URL); |
| 58 | +service.signRequest(accessToken,request); |
| 59 | +try (Responseresponse =service.execute(request)) { |
| 60 | +System.out.println("Got it! Lets see what we found..."); |
| 61 | +System.out.println(); |
| 62 | +System.out.println(response.getCode()); |
| 63 | +System.out.println(response.getBody()); |
| 64 | + } |
| 65 | + |
| 66 | +System.out.println(); |
| 67 | +System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 68 | + |
| 69 | + } |
| 70 | +} |