From 09d725f3a42dd9b9fa6b71dd8f7728bfdda8b971 Mon Sep 17 00:00:00 2001 From: Nishit Patel Date: Wed, 19 Jul 2023 14:09:11 +0530 Subject: [PATCH] add support for zendesk oauth 2.0 Zendesk Oauth Documentation: https://support.zendesk.com/hc/en-us/articles/4408845965210-Using-OAuth-authentication-with-your-application fixes: #1060 --- README.md | 1 + .../github/scribejava/apis/ZendeskApi.java | 43 +++++++++++ .../apis/examples/ZendeskExample.java | 75 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 scribejava-apis/src/main/java/com/github/scribejava/apis/ZendeskApi.java create mode 100644 scribejava-apis/src/test/java/com/github/scribejava/apis/examples/ZendeskExample.java diff --git a/README.md b/README.md index b90ef2e5..abe29d6e 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ ScribeJava support out-of-box several HTTP clients: * Xero (https://www.xero.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/XeroExample.java) * XING (https://www.xing.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/XingExample.java) * Yahoo (https://www.yahoo.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/Yahoo20Example.java), [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/YahooExample.java) +* Zendesk (https://www.zendesk.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/ZendeskExample.java) * check the [examples folder](https://github.com/scribejava/scribejava/tree/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples) ### Small and modular diff --git a/scribejava-apis/src/main/java/com/github/scribejava/apis/ZendeskApi.java b/scribejava-apis/src/main/java/com/github/scribejava/apis/ZendeskApi.java new file mode 100644 index 00000000..2498d7b7 --- /dev/null +++ b/scribejava-apis/src/main/java/com/github/scribejava/apis/ZendeskApi.java @@ -0,0 +1,43 @@ +package com.github.scribejava.apis; + +import com.github.scribejava.apis.openid.OpenIdJsonTokenExtractor; +import com.github.scribejava.core.builder.api.DefaultApi20; +import com.github.scribejava.core.extractors.TokenExtractor; +import com.github.scribejava.core.model.OAuth2AccessToken; + +public class ZendeskApi extends DefaultApi20 { + + private final String serverURL; + private final String accessTokenEndpoint; + private final String authorizationBaseUrl; + + protected ZendeskApi(String serverURL) { + this.serverURL = serverURL; + this.accessTokenEndpoint = serverURL + "/oauth/tokens"; + this.authorizationBaseUrl = serverURL + "/oauth/authorizations/new"; + } + + public static ZendeskApi instance(String serverUrl) { + return new ZendeskApi(serverUrl); + } + + public String getServerURL() { + return serverURL; + } + + @Override + public String getAccessTokenEndpoint() { + return accessTokenEndpoint; + } + + @Override + protected String getAuthorizationBaseUrl() { + return authorizationBaseUrl; + } + + @Override + public TokenExtractor getAccessTokenExtractor() { + return OpenIdJsonTokenExtractor.instance(); + } + +} diff --git a/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/ZendeskExample.java b/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/ZendeskExample.java new file mode 100644 index 00000000..63d88e40 --- /dev/null +++ b/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/ZendeskExample.java @@ -0,0 +1,75 @@ +package com.github.scribejava.apis.examples; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Scanner; +import java.util.concurrent.ExecutionException; + +import com.github.scribejava.apis.ZendeskApi; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.model.OAuthRequest; +import com.github.scribejava.core.model.Response; +import com.github.scribejava.core.model.Verb; +import com.github.scribejava.core.oauth.OAuth20Service; + +@SuppressWarnings("PMD.SystemPrintln") +public class ZendeskExample { + + private static final String NETWORK_NAME = "Zendesk"; + + public static void main(String... args) throws IOException, NoSuchAlgorithmException, KeyManagementException, + InterruptedException, ExecutionException { + + // Replace these with your client id and secret + final String clientId = "Your client ID"; + final String clientSecret = "Your client secret"; + final String clientUrl = "Your zendesk URL"; + final String callbackUrl = "Your callback URL"; + + + final OAuth20Service service = new ServiceBuilder(clientId) + .apiSecret(clientSecret) + .callback(callbackUrl) + .defaultScope("read") + .build(ZendeskApi.instance(clientUrl)); + + final Scanner in = new Scanner(System.in, "UTF-8"); + + System.out.println("=== " + NETWORK_NAME + "'s OAuth 2.0 Workflow ==="); + System.out.println(); + + // Obtain the Authorization URL + System.out.println("Fetching the Authorization URL..."); + final String authorizationUrl = service.getAuthorizationUrl(); + System.out.println("Got the Authorization URL!"); + System.out.println("Now go and authorize ScribeJava here:"); + System.out.println(authorizationUrl); + System.out.println("And paste the authorization code here"); + System.out.print(">> "); + final String code = in.nextLine(); + System.out.println(); + + // Trade the Authorization Code for the Access Token + System.out.println("Trading the Request Token for an Access Token..."); + final OAuth2AccessToken accessToken = service.getAccessToken(code); + System.out.println("Got the Access Token!"); + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); + System.out.println(); + + // Now let's go and ask for a protected resource! + System.out.println("Now we're going to access a protected resource..."); + final OAuthRequest request = new OAuthRequest(Verb.GET, clientUrl + "/api/v2/users/me.json"); + service.signRequest(accessToken, request); + try (Response response = service.execute(request)) { + System.out.println("Got it! Lets see what we found..."); + System.out.println(); + System.out.println(response.getCode()); + System.out.println(response.getBody()); + } + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); + + } + +}