X Tutup
Skip to content

Commit e1f9be5

Browse files
committed
Merge branch 'slackapi' of https://github.com/petrkopotev/scribejava into petrkopotev-slackapi
2 parents 8f48915 + 43ce7ed commit e1f9be5

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.apis.fitbit.FitBitJsonTokenExtractor;
4+
import com.github.scribejava.apis.slack.SlackJsonTokenExtractor;
5+
import com.github.scribejava.apis.slack.SlackOAuth2AccessToken;
6+
import com.github.scribejava.core.builder.api.DefaultApi20;
7+
8+
/**
9+
* Slack.com api
10+
*/
11+
public class SlackApi extends DefaultApi20 {
12+
13+
protected SlackApi() {
14+
}
15+
16+
private static class InstanceHolder {
17+
18+
private static final SlackApi INSTANCE = new SlackApi();
19+
}
20+
21+
public static SlackApi instance() {
22+
return SlackApi.InstanceHolder.INSTANCE;
23+
}
24+
25+
@Override
26+
public String getAccessTokenEndpoint() {
27+
return "https://slack.com/api/oauth.v2.access";
28+
}
29+
30+
@Override
31+
protected String getAuthorizationBaseUrl() {
32+
return "https://slack.com/oauth/v2/authorize";
33+
}
34+
35+
@Override
36+
public SlackJsonTokenExtractor getAccessTokenExtractor() {
37+
return SlackJsonTokenExtractor.instance();
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.scribejava.apis.slack;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
5+
6+
public class SlackJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
7+
8+
protected SlackJsonTokenExtractor() {
9+
}
10+
11+
private static class InstanceHolder {
12+
13+
private static final SlackJsonTokenExtractor INSTANCE = new SlackJsonTokenExtractor();
14+
}
15+
16+
public static SlackJsonTokenExtractor instance() {
17+
return SlackJsonTokenExtractor.InstanceHolder.INSTANCE;
18+
}
19+
20+
@Override
21+
protected SlackOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
22+
String refreshToken, String scope, JsonNode response, String rawResponse) {
23+
String userAccessToken = "";
24+
final JsonNode userAccessTokenNode = response.get("authed_user").get("access_token");
25+
if (userAccessTokenNode != null) {
26+
userAccessToken = userAccessTokenNode.asText();
27+
}
28+
29+
return new SlackOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
30+
userAccessToken, rawResponse);
31+
}
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.scribejava.apis.slack;
2+
3+
import com.github.scribejava.core.model.OAuth2AccessToken;
4+
5+
import java.util.Objects;
6+
7+
public class SlackOAuth2AccessToken extends OAuth2AccessToken {
8+
9+
private final String userAccessToken;
10+
11+
public SlackOAuth2AccessToken(String accessToken, String tokenType, Integer expiresIn, String refreshToken, String scope, String userAccessToken, String rawResponse) {
12+
super(accessToken, tokenType, expiresIn, refreshToken, scope, rawResponse);
13+
this.userAccessToken = userAccessToken;
14+
}
15+
16+
public String getUserAccessToken() {
17+
return userAccessToken;
18+
}
19+
20+
@Override
21+
public int hashCode() {
22+
int hash = super.hashCode();
23+
hash = 37 * hash + Objects.hashCode(userAccessToken);
24+
return hash;
25+
}
26+
27+
@Override
28+
public boolean equals(Object obj) {
29+
if (this == obj) {
30+
return true;
31+
}
32+
if (obj == null) {
33+
return false;
34+
}
35+
if (getClass() != obj.getClass()) {
36+
return false;
37+
}
38+
if (!super.equals(obj)) {
39+
return false;
40+
}
41+
42+
return Objects.equals(userAccessToken, ((SlackOAuth2AccessToken) obj).getUserAccessToken());
43+
}
44+
45+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.SlackApi;
4+
import com.github.scribejava.apis.slack.SlackOAuth2AccessToken;
5+
import com.github.scribejava.core.builder.ServiceBuilder;
6+
import com.github.scribejava.core.model.OAuth2AccessToken;
7+
import com.github.scribejava.core.model.OAuthRequest;
8+
import com.github.scribejava.core.model.Response;
9+
import com.github.scribejava.core.model.Verb;
10+
import com.github.scribejava.core.oauth.OAuth20Service;
11+
12+
import java.io.IOException;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.Scanner;
16+
import java.util.concurrent.ExecutionException;
17+
18+
public class SlackExample {
19+
20+
private static final String NETWORK_NAME = "Slack.com";
21+
private static final String BOT_RESOURCE_URL = "https://slack.com/api/channels.list";
22+
private static final String BOT_SCOPE = "channels:read"
23+
private static final String USER_RESOURCE_URL = "https://slack.com/api/users.list";
24+
private static final String USER_SCOPE = "users:read";
25+
private static final String PAYLOAD = "null";
26+
private static final String CONTENT_TYPE_NAME = "Content-Type";
27+
private static final String CONTENT_TYPE_VALUE = "application/json";
28+
29+
private SlackExample() {
30+
}
31+
32+
@SuppressWarnings("PMD.SystemPrintln")
33+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
34+
// Replace these with your client id and secret
35+
final String clientId = "client-id";
36+
final String clientSecret = "client-secret";
37+
final OAuth20Service service = new ServiceBuilder(clientId)
38+
.apiSecret(clientSecret)
39+
.callback("https://www.example.com/oauth_callback/")
40+
.defaultScope(BOT_SCOPE)
41+
.build(SlackApi.instance());
42+
43+
final Scanner in = new Scanner(System.in);
44+
45+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
46+
System.out.println();
47+
48+
// Obtain the Authorization URL
49+
System.out.println("Fetching the Authorization URL...");
50+
51+
final Map<String, String> additionalParams = new HashMap<>();
52+
// define user scope if any
53+
additionalParams.put("user_scope", USER_SCOPE);
54+
final String authorizationUrl = service.createAuthorizationUrlBuilder()
55+
.additionalParams(additionalParams)
56+
.build();
57+
System.out.println("Got the Authorization URL!");
58+
System.out.println("Now go and authorize ScribeJava here:");
59+
System.out.println(authorizationUrl);
60+
System.out.println("And paste the authorization code here");
61+
System.out.print(">>");
62+
final String code = in.nextLine();
63+
System.out.println();
64+
65+
System.out.println("Trading the Authorization Code for an Access Token...");
66+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
67+
System.out.println("Got the Access Token!");
68+
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
69+
System.out.println();
70+
71+
System.out.println("Getting info using BOT token...");
72+
final OAuthRequest request = new OAuthRequest(Verb.GET, BOT_RESOURCE_URL);
73+
request.addHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_VALUE);
74+
request.setPayload(PAYLOAD);
75+
service.signRequest(accessToken, request);
76+
77+
try (Response response = service.execute(request)) {
78+
System.out.println("Got it! Lets see what we found...");
79+
System.out.println();
80+
System.out.println(response.getCode());
81+
System.out.println(response.getBody());
82+
System.out.println();
83+
}
84+
85+
System.out.println("Getting info using USER token...");
86+
final OAuthRequest userRequest = new OAuthRequest(Verb.GET, USER_RESOURCE_URL);
87+
userRequest.addHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_VALUE);
88+
userRequest.setPayload(PAYLOAD);
89+
SlackOAuth2AccessToken token = (SlackOAuth2AccessToken)accessToken;
90+
service.signRequest(token.getUserAccessToken(), userRequest);
91+
92+
try (Response response = service.execute(userRequest)) {
93+
System.out.println("Got it! Lets see what we found...");
94+
System.out.println();
95+
System.out.println(response.getCode());
96+
System.out.println(response.getBody());
97+
}
98+
99+
100+
System.out.println();
101+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
102+
}
103+
}

0 commit comments

Comments
 (0)
X Tutup