|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.security.KeyManagementException; |
| 5 | +import java.security.NoSuchAlgorithmException; |
| 6 | +import java.util.Scanner; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | + |
| 9 | +import com.github.scribejava.apis.ZendeskApi; |
| 10 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 11 | +import com.github.scribejava.core.model.OAuth2AccessToken; |
| 12 | +import com.github.scribejava.core.model.OAuthRequest; |
| 13 | +import com.github.scribejava.core.model.Response; |
| 14 | +import com.github.scribejava.core.model.Verb; |
| 15 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 16 | + |
| 17 | +@SuppressWarnings("PMD.SystemPrintln") |
| 18 | +public class ZendeskExample { |
| 19 | + |
| 20 | + private static final String NETWORK_NAME = "Zendesk"; |
| 21 | + |
| 22 | + public static void main(String... args) throws IOException, NoSuchAlgorithmException, KeyManagementException, |
| 23 | + InterruptedException, ExecutionException { |
| 24 | + |
| 25 | + // Replace these with your client id and secret |
| 26 | + final String clientId = "Your client ID"; |
| 27 | + final String clientSecret = "Your client secret"; |
| 28 | + final String clientUrl = "Your zendesk URL"; |
| 29 | + final String callbackUrl = "Your callback URL"; |
| 30 | + |
| 31 | + |
| 32 | + final OAuth20Service service = new ServiceBuilder(clientId) |
| 33 | + .apiSecret(clientSecret) |
| 34 | + .callback(callbackUrl) |
| 35 | + .defaultScope("read") |
| 36 | + .build(ZendeskApi.instance(clientUrl)); |
| 37 | + |
| 38 | + final Scanner in = new Scanner(System.in, "UTF-8"); |
| 39 | + |
| 40 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth 2.0 Workflow ==="); |
| 41 | + System.out.println(); |
| 42 | + |
| 43 | + // Obtain the Authorization URL |
| 44 | + System.out.println("Fetching the Authorization URL..."); |
| 45 | + final String authorizationUrl = service.getAuthorizationUrl(); |
| 46 | + System.out.println("Got the Authorization URL!"); |
| 47 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 48 | + System.out.println(authorizationUrl); |
| 49 | + System.out.println("And paste the authorization code here"); |
| 50 | + System.out.print(">> "); |
| 51 | + final String code = in.nextLine(); |
| 52 | + System.out.println(); |
| 53 | + |
| 54 | + // Trade the Authorization Code for the Access Token |
| 55 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 56 | + final OAuth2AccessToken accessToken = service.getAccessToken(code); |
| 57 | + System.out.println("Got the Access Token!"); |
| 58 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); |
| 59 | + System.out.println(); |
| 60 | + |
| 61 | + // Now let's go and ask for a protected resource! |
| 62 | + System.out.println("Now we're going to access a protected resource..."); |
| 63 | + final OAuthRequest request = new OAuthRequest(Verb.GET, clientUrl + "/api/v2/users/me.json"); |
| 64 | + service.signRequest(accessToken, request); |
| 65 | + try (Response response = service.execute(request)) { |
| 66 | + System.out.println("Got it! Lets see what we found..."); |
| 67 | + System.out.println(); |
| 68 | + System.out.println(response.getCode()); |
| 69 | + System.out.println(response.getBody()); |
| 70 | + } |
| 71 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments