Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for zendesk oauth 2.0 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OAuth2AccessToken> getAccessTokenExtractor() {
return OpenIdJsonTokenExtractor.instance();
}

}
Original file line number Diff line number Diff line change
@@ -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! :)");

}

}