|
| 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