|
| 1 | +package com.github.scribejava.apis.examples; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | +import com.github.scribejava.core.builder.ServiceBuilder; |
| 5 | +import com.github.scribejava.apis.VkontakteApi; |
| 6 | +import com.github.scribejava.core.model.ForceTypeOfHttpRequest; |
| 7 | +import com.github.scribejava.core.model.OAuth2AccessToken; |
| 8 | +import com.github.scribejava.core.model.OAuthRequestAsync; |
| 9 | +import com.github.scribejava.core.model.Response; |
| 10 | +import com.github.scribejava.core.model.ScribeJavaConfig; |
| 11 | +import com.github.scribejava.core.model.Verb; |
| 12 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 13 | +import com.github.scribejava.httpclient.ahc.AhcHttpClient; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.concurrent.ExecutionException; |
| 16 | +import org.asynchttpclient.DefaultAsyncHttpClient; |
| 17 | +import org.asynchttpclient.DefaultAsyncHttpClientConfig; |
| 18 | + |
| 19 | +public final class VkontakteExternalHttpExample { |
| 20 | + |
| 21 | + private static final String NETWORK_NAME = "Vkontakte.ru"; |
| 22 | + private static final String PROTECTED_RESOURCE_URL = "https://api.vk.com/method/users.get"; |
| 23 | + |
| 24 | + private VkontakteExternalHttpExample() { |
| 25 | + } |
| 26 | + |
| 27 | + public static void main(String... args) throws IOException, InterruptedException, ExecutionException { |
| 28 | + // Replace these with your client id and secret |
| 29 | + final String clientId = "your client id"; |
| 30 | + final String clientSecret = "your client secret"; |
| 31 | + ScribeJavaConfig.setForceTypeOfHttpRequests(ForceTypeOfHttpRequest.FORCE_ASYNC_ONLY_HTTP_REQUESTS); |
| 32 | + |
| 33 | + //create any http cleint externally |
| 34 | + final DefaultAsyncHttpClientConfig httpClientConfig = new DefaultAsyncHttpClientConfig.Builder() |
| 35 | + .setMaxConnections(5) |
| 36 | + .setRequestTimeout(10_000) |
| 37 | + .setPooledConnectionIdleTimeout(1_000) |
| 38 | + .setReadTimeout(1_000) |
| 39 | + .build(); |
| 40 | + //wrap it |
| 41 | + try (DefaultAsyncHttpClient ahcHttpClient = new DefaultAsyncHttpClient(httpClientConfig)) { |
| 42 | + //wrap it |
| 43 | + final AhcHttpClient wrappedAHCHttpClient = new AhcHttpClient(ahcHttpClient); |
| 44 | + |
| 45 | + final OAuth20Service service = new ServiceBuilder() |
| 46 | + .httpClient(wrappedAHCHttpClient) |
| 47 | + .apiKey(clientId) |
| 48 | + .apiSecret(clientSecret) |
| 49 | + .scope("wall,offline") // replace with desired scope |
| 50 | + .callback("http://your.site.com/callback") |
| 51 | + .build(VkontakteApi.instance()); |
| 52 | + final Scanner in = new Scanner(System.in); |
| 53 | + |
| 54 | + System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); |
| 55 | + System.out.println(); |
| 56 | + |
| 57 | + // Obtain the Authorization URL |
| 58 | + System.out.println("Fetching the Authorization URL..."); |
| 59 | + final String authorizationUrl = service.getAuthorizationUrl(); |
| 60 | + System.out.println("Got the Authorization URL!"); |
| 61 | + System.out.println("Now go and authorize ScribeJava here:"); |
| 62 | + System.out.println(authorizationUrl); |
| 63 | + System.out.println("And paste the authorization code here"); |
| 64 | + System.out.print(">>"); |
| 65 | + final String code = in.nextLine(); |
| 66 | + System.out.println(); |
| 67 | + |
| 68 | + // Trade the Request Token and Verfier for the Access Token |
| 69 | + System.out.println("Trading the Request Token for an Access Token..."); |
| 70 | + final OAuth2AccessToken accessToken = service.getAccessTokenAsync(code, null).get(); |
| 71 | + System.out.println("Got the Access Token!"); |
| 72 | + System.out.println("(if your curious it looks like this: " + accessToken |
| 73 | + + ", 'rawResponse'='" + accessToken.getRawResponse() + "')"); |
| 74 | + System.out.println(); |
| 75 | + |
| 76 | + // Now let's go and ask for a protected resource! |
| 77 | + System.out.println("Now we're going to access a protected resource..."); |
| 78 | + final OAuthRequestAsync request = new OAuthRequestAsync(Verb.GET, PROTECTED_RESOURCE_URL, service); |
| 79 | + service.signRequest(accessToken, request); |
| 80 | + final Response response = request.sendAsync(null).get(); |
| 81 | + System.out.println("Got it! Lets see what we found..."); |
| 82 | + System.out.println(); |
| 83 | + System.out.println(response.getCode()); |
| 84 | + System.out.println(response.getBody()); |
| 85 | + |
| 86 | + System.out.println(); |
| 87 | + System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)"); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments