From e849e8291c6d1e3f25e9eecacf5de754c6ec0600 Mon Sep 17 00:00:00 2001 From: Aly M Date: Mon, 31 Jan 2022 06:59:16 -0800 Subject: [PATCH 1/2] Add Atlassian Api. --- README.md | 2 + .../github/scribejava/apis/AtlassianApi.java | 28 +++++++ .../apis/examples/AtlassianExample.java | 82 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 scribejava-apis/src/main/java/com/github/scribejava/apis/AtlassianApi.java create mode 100644 scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java diff --git a/README.md b/README.md index ef4083367..758681948 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ ScribeJava support out-of-box several HTTP clients: ### Supports all (50+) major 1.0a and 2.0 OAuth APIs out-of-the-box +* Atlassian (https://developer.atlassian.com) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java) + * Asana (https://asana.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AsanaExample.java) * Automatic (https://www.automatic.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AutomaticExample.java) * AWeber (http://www.aweber.com/) [example](https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AWeberExample.java) diff --git a/scribejava-apis/src/main/java/com/github/scribejava/apis/AtlassianApi.java b/scribejava-apis/src/main/java/com/github/scribejava/apis/AtlassianApi.java new file mode 100644 index 000000000..70b1703e8 --- /dev/null +++ b/scribejava-apis/src/main/java/com/github/scribejava/apis/AtlassianApi.java @@ -0,0 +1,28 @@ +package com.github.scribejava.apis; + +import com.github.scribejava.core.builder.api.DefaultApi20; + +public class AtlassianApi extends DefaultApi20 { + + protected AtlassianApi() { + } + + private static class InstanceHolder { + + private static final AtlassianApi INSTANCE = new AtlassianApi(); + } + + public static AtlassianApi instance() { + return InstanceHolder.INSTANCE; + } + + @Override + public String getAccessTokenEndpoint() { + return "https://auth.atlassian.com/oauth/token"; + } + + @Override + protected String getAuthorizationBaseUrl() { + return "https://auth.atlassian.com/authorize"; + } +} diff --git a/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java b/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java new file mode 100644 index 000000000..f115c50c4 --- /dev/null +++ b/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java @@ -0,0 +1,82 @@ +package com.github.scribejava.apis.examples; + +import com.github.scribejava.apis.AtlassianApi; +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; + +import java.io.IOException; +import java.util.Random; +import java.util.Scanner; +import java.util.concurrent.ExecutionException; + +public class AtlassianExample { + + private static final String PROTECTED_RESOURCE_URL = "https://api.atlassian.com/oauth/token/accessible-resources"; + + private AsanaExample() { + } + + @SuppressWarnings("PMD.SystemPrintln") + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { + final String apiKey = "your client id"; + final String apiSecret = "your client secret"; + final String secretState = "secret" + new Random().nextInt(999_999); + final OAuth20Service service = new ServiceBuilder(apiKey) + .apiSecret(apiSecret) + .callback("https://localhost/") + .build(AtlassianApi.instance()); + final Scanner in = new Scanner(System.in); + + // Obtain Auth URL + System.out.println("Fetching the Authorication URL..."); + System.out.println("Got the Authorization URL!"); + final String authorizationUrl = service.getAuthorizationUrl(secretState); + 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(); + + System.out.println("Trading the Authorization Code for an Access Token..."); + 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(); + + System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'."); + System.out.print(">>"); + final String value = in.nextLine(); + if (secretState.equals(value)) { + System.out.println("State value does match!"); + } else { + System.out.println("Ooops, state value does not match!"); + System.out.println("Expected = " + secretState); + System.out.println("Got = " + value); + System.out.println(); + } + + System.out.println("Refreshing the Access Token..."); + accessToken = service.refreshAccessToken(accessToken.getRefreshToken()); + System.out.println("Refreshed the Access Token!"); + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')"); + System.out.println(); + + System.out.println("Now we're going to access a protected resource..."); + final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); + 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(); + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); + } +} From c6a80982d0549f1de7951ac011c6113627400cb2 Mon Sep 17 00:00:00 2001 From: Aly M Date: Mon, 31 Jan 2022 07:01:01 -0800 Subject: [PATCH 2/2] Add Atlassian Api. --- .../com/github/scribejava/apis/examples/AtlassianExample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java b/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java index f115c50c4..493105b6f 100644 --- a/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java +++ b/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/AtlassianExample.java @@ -15,7 +15,7 @@ public class AtlassianExample { - private static final String PROTECTED_RESOURCE_URL = "https://api.atlassian.com/oauth/token/accessible-resources"; + private static final String PROTECTED_RESOURCE_URL = "https://api.atlassian.com/me"; private AsanaExample() { }