Search in sources :

Example 6 with OAuth2AccessToken

use of com.github.scribejava.core.model.OAuth2AccessToken in project scribejava by scribejava.

the class ViadeoExample method main.

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
    // Replace these with your own api key and secret
    final String apiKey = "your_app_id";
    final String apiSecret = "your_api_secret";
    final OAuth20Service service = new ServiceBuilder().apiKey(apiKey).apiSecret(apiSecret).callback("http://www.example.com/oauth_callback/").build(ViadeoApi.instance());
    final Scanner in = new Scanner(System.in);
    System.out.println("=== " + NETWORK_NAME + "'s OAuth 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 Request Token and Verfier 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("(if your curious it looks like this: " + accessToken + ", 'rawResponse'='" + 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, PROTECTED_RESOURCE_URL);
    service.signRequest(accessToken, request);
    final 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! :)");
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Scanner(java.util.Scanner) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 7 with OAuth2AccessToken

use of com.github.scribejava.core.model.OAuth2AccessToken in project scribejava by scribejava.

the class VkontakteExample method main.

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
    // Replace these with your client id and secret
    final String clientId = "your client id";
    final String clientSecret = "your client secret";
    final OAuth20Service service = new ServiceBuilder().apiKey(clientId).apiSecret(clientSecret).scope(// replace with desired scope
    "wall,offline").callback("http://your.site.com/callback").build(VkontakteApi.instance());
    final Scanner in = new Scanner(System.in);
    System.out.println("=== " + NETWORK_NAME + "'s OAuth 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 Request Token and Verfier 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("(if your curious it looks like this: " + accessToken + ", 'rawResponse'='" + 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, PROTECTED_RESOURCE_URL);
    service.signRequest(accessToken, request);
    final 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! :)");
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Scanner(java.util.Scanner) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 8 with OAuth2AccessToken

use of com.github.scribejava.core.model.OAuth2AccessToken in project scribejava by scribejava.

the class VkontakteExternalHttpExample method main.

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
    // Replace these with your client id and secret
    final String clientId = "your client id";
    final String clientSecret = "your client secret";
    //create any http client externally
    final DefaultAsyncHttpClientConfig httpClientConfig = new DefaultAsyncHttpClientConfig.Builder().setMaxConnections(5).setRequestTimeout(10_000).setPooledConnectionIdleTimeout(1_000).setReadTimeout(1_000).build();
    //wrap it
    try (DefaultAsyncHttpClient ahcHttpClient = new DefaultAsyncHttpClient(httpClientConfig)) {
        //wrap it
        final AhcHttpClient wrappedAHCHttpClient = new AhcHttpClient(ahcHttpClient);
        final OAuth20Service service = new ServiceBuilder().httpClient(wrappedAHCHttpClient).apiKey(clientId).apiSecret(clientSecret).scope(// replace with desired scope
        "wall,offline").callback("http://your.site.com/callback").build(VkontakteApi.instance());
        final Scanner in = new Scanner(System.in);
        System.out.println("=== " + NETWORK_NAME + "'s OAuth 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 Request Token and Verfier 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("(if your curious it looks like this: " + accessToken + ", 'rawResponse'='" + 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, PROTECTED_RESOURCE_URL);
        service.signRequest(accessToken, request);
        final 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! :)");
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Scanner(java.util.Scanner) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AhcHttpClient(com.github.scribejava.httpclient.ahc.AhcHttpClient) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Example 9 with OAuth2AccessToken

use of com.github.scribejava.core.model.OAuth2AccessToken in project scribejava by scribejava.

the class OAuth2AccessTokenExtractorTest method shouldExtractTokenFromOAuthStandardResponse.

@Test
public void shouldExtractTokenFromOAuthStandardResponse() throws IOException {
    final String response = "access_token=166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159" + "|RsXNdKrpxg8L6QNLWcs2TVTmcaE";
    final OAuth2AccessToken extracted = extractor.extract(ok(response));
    assertEquals("166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159|RsXNdKrpxg8L6QNLWcs2TVTmcaE", extracted.getAccessToken());
}
Also used : OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Test(org.junit.Test)

Example 10 with OAuth2AccessToken

use of com.github.scribejava.core.model.OAuth2AccessToken in project scribejava by scribejava.

the class OAuth2AccessTokenExtractorTest method shouldExtractTokenFromResponseWithExpiresAndRefreshParam.

@Test
public void shouldExtractTokenFromResponseWithExpiresAndRefreshParam() throws IOException {
    final String response = "access_token=166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159" + "|RsXNdKrpxg8L6QNLWcs2TVTmcaE&expires_in=5108&token_type=bearer&refresh_token=166942940015970";
    final OAuth2AccessToken extracted = extractor.extract(ok(response));
    assertEquals("166942940015970|2.2ltzWXYNDjCtg5ZDVVJJeg__.3600.1295816400-548517159|RsXNdKrpxg8L6QNLWcs2TVTmcaE", extracted.getAccessToken());
    assertEquals(Integer.valueOf(5108), extracted.getExpiresIn());
    assertEquals("bearer", extracted.getTokenType());
    assertEquals("166942940015970", extracted.getRefreshToken());
}
Also used : OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Test(org.junit.Test)

Aggregations

OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)37 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)31 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)30 Response (com.github.scribejava.core.model.Response)29 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)29 Scanner (java.util.Scanner)29 Random (java.util.Random)11 Test (org.junit.Test)8 HashMap (java.util.HashMap)4 NingHttpClientConfig (com.github.scribejava.httpclient.ning.NingHttpClientConfig)2 Gson (com.google.gson.Gson)2 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)2 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)2 HttpClientConfig (com.github.scribejava.core.httpclient.HttpClientConfig)1 AhcHttpClient (com.github.scribejava.httpclient.ahc.AhcHttpClient)1 AhcHttpClientConfig (com.github.scribejava.httpclient.ahc.AhcHttpClientConfig)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)1