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