|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import java.util.Scanner; |
| 5 | +import com.github.scribejava.apis.GoogleApi20; |
| 6 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 7 | +import com.github.scribejava.core.model.OAuth2AccessToken; |
| 8 | +import com.github.scribejava.core.model.OAuthRequest; |
| 9 | +import com.github.scribejava.core.model.Response; |
| 10 | +import com.github.scribejava.core.model.Verb; |
| 11 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 12 | +import com.github.scribejava.httpclient.armeria.ArmeriaHttpClientConfig; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | + |
| 18 | +public class Google20ArmeriaExample { |
| 19 | + |
| 20 | + private static final String NETWORK_NAME = "Google Armeria"; |
| 21 | + private static final String PROTECTED_RESOURCE_URL = "https://www.googleapis.com/oauth2/v3/userinfo"; |
| 22 | + |
| 23 | + private Google20ArmeriaExample() { |
| 24 | + } |
| 25 | + |
| 26 | + @SuppressWarnings("PMD.SystemPrintln") |
| 27 | + public static void main(String... args) throws InterruptedException, ExecutionException, IOException { |
| 28 | + // Replace these with your client id and secret |
| 29 | + final String clientId = "your client id"; |
| 30 | + final String clientSecret = "your client secret"; |
| 31 | + final String secretState = "secret" + new Random().nextInt(999_999); |
| 32 | + |
| 33 | + try (OAuth20Service service = new ServiceBuilder(clientId) |
| 34 | + .apiSecret(clientSecret) |
| 35 | + .defaultScope("profile") // replace with desired scope |
| 36 | + .callback("http://example.com/callback") |
| 37 | + .httpClientConfig(ArmeriaHttpClientConfig.defaultConfig()) |
| 38 | + .build(GoogleApi20.instance())) { |
| 39 | + final Scanner in = new Scanner(System.in, "UTF-8"); |
| 40 | + |
| 41 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); |
| 42 | + System.out.println(); |
| 43 | + |
| 44 | + // Obtain the Authorization URL |
| 45 | + System.out.println("Fetching the Authorization URL..."); |
| 46 | + //pass access_type=offline to get refresh token |
| 47 | + //https://developers.google.com/identity/protocols/OAuth2WebServer#preparing-to-start-the-oauth-20-flow |
| 48 | + final Map<String, String> additionalParams = new HashMap<>(); |
| 49 | + additionalParams.put("access_type", "offline"); |
| 50 | + //force to reget refresh token (if user are asked not the first time) |
| 51 | + additionalParams.put("prompt", "consent"); |
| 52 | + final String authorizationUrl = service.createAuthorizationUrlBuilder() |
| 53 | + .state(secretState) |
| 54 | + .additionalParams(additionalParams) |
| 55 | + .build(); |
| 56 | + System.out.println("Got the Authorization URL!"); |
| 57 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 58 | + System.out.println(authorizationUrl); |
| 59 | + System.out.println("And paste the authorization code here"); |
| 60 | + System.out.print(">>"); |
| 61 | + final String code = in.nextLine(); |
| 62 | + System.out.println(); |
| 63 | + |
| 64 | + System.out.println("And paste the state from server here. We have set 'secretState'='" |
| 65 | + + secretState + "'."); |
| 66 | + System.out.print(">>"); |
| 67 | + final String value = in.nextLine(); |
| 68 | + if (secretState.equals(value)) { |
| 69 | + System.out.println("State value does match!"); |
| 70 | + } else { |
| 71 | + System.out.println("Ooops, state value does not match!"); |
| 72 | + System.out.println("Expected = " + secretState); |
| 73 | + System.out.println("Got = " + value); |
| 74 | + System.out.println(); |
| 75 | + } |
| 76 | + |
| 77 | + System.out.println("Trading the Authorization Code for an Access Token..."); |
| 78 | + OAuth2AccessToken accessToken = service.getAccessToken(code); |
| 79 | + System.out.println("Got the Access Token!"); |
| 80 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() |
| 81 | + + "')"); |
| 82 | + |
| 83 | + System.out.println("Refreshing the Access Token..."); |
| 84 | + accessToken = service.refreshAccessToken(accessToken.getRefreshToken()); |
| 85 | + System.out.println("Refreshed the Access Token!"); |
| 86 | + System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() |
| 87 | + + "')"); |
| 88 | + System.out.println(); |
| 89 | + |
| 90 | + // Now let's go and ask for a protected resource! |
| 91 | + System.out.println("Now we're going to access a protected resource..."); |
| 92 | + while (true) { |
| 93 | + System.out.println("Paste fieldnames to fetch (leave empty to get profile, 'exit' to stop example)"); |
| 94 | + System.out.print(">>"); |
| 95 | + final String query = in.nextLine(); |
| 96 | + System.out.println(); |
| 97 | + |
| 98 | + final String requestUrl; |
| 99 | + if ("exit".equals(query)) { |
| 100 | + break; |
| 101 | + } else if (query == null || query.isEmpty()) { |
| 102 | + requestUrl = PROTECTED_RESOURCE_URL; |
| 103 | + } else { |
| 104 | + requestUrl = PROTECTED_RESOURCE_URL + "?fields=" + query; |
| 105 | + } |
| 106 | + |
| 107 | + final OAuthRequest request = new OAuthRequest(Verb.GET, requestUrl); |
| 108 | + service.signRequest(accessToken, request); |
| 109 | + System.out.println(); |
| 110 | + try (Response response = service.execute(request)) { |
| 111 | + System.out.println(response.getCode()); |
| 112 | + System.out.println(response.getBody()); |
| 113 | + } |
| 114 | + System.out.println(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments