|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import com.github.scribejava.apis.KakaoApi; |
| 4 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +import com.github.scribejava.core.model.OAuth2AccessToken; |
| 6 | +import com.github.scribejava.core.model.OAuthRequest; |
| 7 | +import com.github.scribejava.core.model.Response; |
| 8 | +import com.github.scribejava.core.model.Verb; |
| 9 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Scanner; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | + |
| 15 | +public class KakaoExample { |
| 16 | + |
| 17 | + private static final String NETWORK_NAME = "Kakao"; |
| 18 | + private static final String PROTECTED_RESOURCE_URL = "https://kapi.kakao.com/v2/user/me"; |
| 19 | + |
| 20 | + private KakaoExample() { |
| 21 | + } |
| 22 | + |
| 23 | + @SuppressWarnings("PMD.SystemPrintln") |
| 24 | + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { |
| 25 | + // Replace these with your client id and secret |
| 26 | + final String clientId = "your client id"; |
| 27 | + |
| 28 | + final OAuth20Service service = new ServiceBuilder(clientId) |
| 29 | + .callback("http://www.example.com/oauth_callback/") |
| 30 | + .build(KakaoApi.instance()); |
| 31 | + |
| 32 | + final Scanner in = new Scanner(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 | + final String authorizationUrl = 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 | + final String code = in.nextLine(); |
| 46 | + System.out.println(); |
| 47 | + |
| 48 | + System.out.println("Trading the Authorization Code for an Access Token..."); |
| 49 | + final OAuth2AccessToken accessToken = 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 | + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); |
| 58 | + service.signRequest(accessToken, request); |
| 59 | + try (Response response = 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 | +} |
0 commit comments