Skip to content

Commit a8578ef

Browse files
committed
Add Kakao API
1 parent a7b01f6 commit a8578ef

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
5+
import com.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
6+
7+
public class KakaoApi extends DefaultApi20 {
8+
9+
protected KakaoApi() {
10+
}
11+
12+
private static class InstanceHolder {
13+
private static final KakaoApi INSTANCE = new KakaoApi();
14+
}
15+
16+
public static KakaoApi instance() {
17+
return KakaoApi.InstanceHolder.INSTANCE;
18+
}
19+
20+
@Override
21+
public String getAccessTokenEndpoint() {
22+
return "https://kauth.kakao.com/oauth/token";
23+
}
24+
25+
@Override
26+
protected String getAuthorizationBaseUrl() {
27+
return "https://kauth.kakao.com/oauth/authorize";
28+
}
29+
30+
@Override
31+
public ClientAuthentication getClientAuthentication() {
32+
return RequestBodyAuthenticationScheme.instance();
33+
}
34+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)