Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite1f9be5

Browse files
committed
Merge branch 'slackapi' ofhttps://github.com/petrkopotev/scribejava into petrkopotev-slackapi
2 parents8f48915 +43ce7ed commite1f9be5

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+
packagecom.github.scribejava.apis;
2+
3+
importcom.github.scribejava.apis.fitbit.FitBitJsonTokenExtractor;
4+
importcom.github.scribejava.apis.slack.SlackJsonTokenExtractor;
5+
importcom.github.scribejava.apis.slack.SlackOAuth2AccessToken;
6+
importcom.github.scribejava.core.builder.api.DefaultApi20;
7+
8+
/**
9+
* Slack.com api
10+
*/
11+
publicclassSlackApiextendsDefaultApi20 {
12+
13+
protectedSlackApi() {
14+
}
15+
16+
privatestaticclassInstanceHolder {
17+
18+
privatestaticfinalSlackApiINSTANCE =newSlackApi();
19+
}
20+
21+
publicstaticSlackApiinstance() {
22+
returnSlackApi.InstanceHolder.INSTANCE;
23+
}
24+
25+
@Override
26+
publicStringgetAccessTokenEndpoint() {
27+
return"https://slack.com/api/oauth.v2.access";
28+
}
29+
30+
@Override
31+
protectedStringgetAuthorizationBaseUrl() {
32+
return"https://slack.com/oauth/v2/authorize";
33+
}
34+
35+
@Override
36+
publicSlackJsonTokenExtractorgetAccessTokenExtractor() {
37+
returnSlackJsonTokenExtractor.instance();
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.github.scribejava.apis.slack;
2+
3+
importcom.fasterxml.jackson.databind.JsonNode;
4+
importcom.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
5+
6+
publicclassSlackJsonTokenExtractorextendsOAuth2AccessTokenJsonExtractor {
7+
8+
protectedSlackJsonTokenExtractor() {
9+
}
10+
11+
privatestaticclassInstanceHolder {
12+
13+
privatestaticfinalSlackJsonTokenExtractorINSTANCE =newSlackJsonTokenExtractor();
14+
}
15+
16+
publicstaticSlackJsonTokenExtractorinstance() {
17+
returnSlackJsonTokenExtractor.InstanceHolder.INSTANCE;
18+
}
19+
20+
@Override
21+
protectedSlackOAuth2AccessTokencreateToken(StringaccessToken,StringtokenType,IntegerexpiresIn,
22+
StringrefreshToken,Stringscope,JsonNoderesponse,StringrawResponse) {
23+
StringuserAccessToken ="";
24+
finalJsonNodeuserAccessTokenNode =response.get("authed_user").get("access_token");
25+
if (userAccessTokenNode !=null) {
26+
userAccessToken =userAccessTokenNode.asText();
27+
}
28+
29+
returnnewSlackOAuth2AccessToken(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+
packagecom.github.scribejava.apis.slack;
2+
3+
importcom.github.scribejava.core.model.OAuth2AccessToken;
4+
5+
importjava.util.Objects;
6+
7+
publicclassSlackOAuth2AccessTokenextendsOAuth2AccessToken {
8+
9+
privatefinalStringuserAccessToken;
10+
11+
publicSlackOAuth2AccessToken(StringaccessToken,StringtokenType,IntegerexpiresIn,StringrefreshToken,Stringscope,StringuserAccessToken,StringrawResponse) {
12+
super(accessToken,tokenType,expiresIn,refreshToken,scope,rawResponse);
13+
this.userAccessToken =userAccessToken;
14+
}
15+
16+
publicStringgetUserAccessToken() {
17+
returnuserAccessToken;
18+
}
19+
20+
@Override
21+
publicinthashCode() {
22+
inthash =super.hashCode();
23+
hash =37 *hash +Objects.hashCode(userAccessToken);
24+
returnhash;
25+
}
26+
27+
@Override
28+
publicbooleanequals(Objectobj) {
29+
if (this ==obj) {
30+
returntrue;
31+
}
32+
if (obj ==null) {
33+
returnfalse;
34+
}
35+
if (getClass() !=obj.getClass()) {
36+
returnfalse;
37+
}
38+
if (!super.equals(obj)) {
39+
returnfalse;
40+
}
41+
42+
returnObjects.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+
packagecom.github.scribejava.apis.examples;
2+
3+
importcom.github.scribejava.apis.SlackApi;
4+
importcom.github.scribejava.apis.slack.SlackOAuth2AccessToken;
5+
importcom.github.scribejava.core.builder.ServiceBuilder;
6+
importcom.github.scribejava.core.model.OAuth2AccessToken;
7+
importcom.github.scribejava.core.model.OAuthRequest;
8+
importcom.github.scribejava.core.model.Response;
9+
importcom.github.scribejava.core.model.Verb;
10+
importcom.github.scribejava.core.oauth.OAuth20Service;
11+
12+
importjava.io.IOException;
13+
importjava.util.HashMap;
14+
importjava.util.Map;
15+
importjava.util.Scanner;
16+
importjava.util.concurrent.ExecutionException;
17+
18+
publicclassSlackExample {
19+
20+
privatestaticfinalStringNETWORK_NAME ="Slack.com";
21+
privatestaticfinalStringBOT_RESOURCE_URL ="https://slack.com/api/channels.list";
22+
privatestaticfinalStringBOT_SCOPE ="channels:read"
23+
privatestaticfinalStringUSER_RESOURCE_URL ="https://slack.com/api/users.list";
24+
privatestaticfinalStringUSER_SCOPE ="users:read";
25+
privatestaticfinalStringPAYLOAD ="null";
26+
privatestaticfinalStringCONTENT_TYPE_NAME ="Content-Type";
27+
privatestaticfinalStringCONTENT_TYPE_VALUE ="application/json";
28+
29+
privateSlackExample() {
30+
}
31+
32+
@SuppressWarnings("PMD.SystemPrintln")
33+
publicstaticvoidmain(String...args)throwsIOException,InterruptedException,ExecutionException {
34+
// Replace these with your client id and secret
35+
finalStringclientId ="client-id";
36+
finalStringclientSecret ="client-secret";
37+
finalOAuth20Serviceservice =newServiceBuilder(clientId)
38+
.apiSecret(clientSecret)
39+
.callback("https://www.example.com/oauth_callback/")
40+
.defaultScope(BOT_SCOPE)
41+
.build(SlackApi.instance());
42+
43+
finalScannerin =newScanner(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+
finalMap<String,String>additionalParams =newHashMap<>();
52+
// define user scope if any
53+
additionalParams.put("user_scope",USER_SCOPE);
54+
finalStringauthorizationUrl =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+
finalStringcode =in.nextLine();
63+
System.out.println();
64+
65+
System.out.println("Trading the Authorization Code for an Access Token...");
66+
finalOAuth2AccessTokenaccessToken =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+
finalOAuthRequestrequest =newOAuthRequest(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 (Responseresponse =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+
finalOAuthRequestuserRequest =newOAuthRequest(Verb.GET,USER_RESOURCE_URL);
87+
userRequest.addHeader(CONTENT_TYPE_NAME,CONTENT_TYPE_VALUE);
88+
userRequest.setPayload(PAYLOAD);
89+
SlackOAuth2AccessTokentoken = (SlackOAuth2AccessToken)accessToken;
90+
service.signRequest(token.getUserAccessToken(),userRequest);
91+
92+
try (Responseresponse =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)

[8]ページ先頭

©2009-2025 Movatter.jp