Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 9 July 2016

Facebook OAuth 2.0 Example using Scribe Java with asyncNingHttpClientConfig

In this example we are going to see about how to use Facebook OAuth 2.0 using Scribe Java with asyncNingHttpClientConfig

Facebook OAuth Example with asyncNingHttpClientConfig

FacebookAsyncNingExample.java

01.import com.ning.http.client.AsyncHttpClientConfig;
02.import java.util.Random;
03.import java.util.Scanner;
04.import java.util.concurrent.ExecutionException;
05.import com.github.scribejava.apis.FacebookApi;
06.import com.github.scribejava.core.builder.ServiceBuilder;
07.import com.github.scribejava.core.model.ForceTypeOfHttpRequest;
08.import com.github.scribejava.core.model.OAuth2AccessToken;
09.import com.github.scribejava.core.model.OAuthRequestAsync;
10.import com.github.scribejava.core.model.Response;
11.import com.github.scribejava.core.model.ScribeJavaConfig;
12.import com.github.scribejava.core.model.Verb;
13.import com.github.scribejava.core.oauth.OAuth20Service;
14.import java.io.IOException;
15. 
16.public abstract class FacebookAsyncNingExample {
17. 
18.    private static final String NETWORK_NAME = "Facebook";
19.    private static final String PROTECTED_RESOURCE_URL = "http://ift.tt/29qmF1n";
20. 
21.    public static void main(String... args) throws InterruptedException, ExecutionException, IOException {
22.        // Replace these with your client id and secret
23.        final String clientId = "your client id";
24.        final String clientSecret = "your client secret";
25.        final String secretState = "secret" + new Random().nextInt(999_999);
26.        ScribeJavaConfig.setForceTypeOfHttpRequests(ForceTypeOfHttpRequest.FORCE_ASYNC_ONLY_HTTP_REQUESTS);
27.        final AsyncHttpClientConfig clientConfig = new AsyncHttpClientConfig.Builder()
28.                .setMaxConnections(5)
29.                .setRequestTimeout(10_000)
30.                .setAllowPoolingConnections(false)
31.                .setPooledConnectionIdleTimeout(1_000)
32.                .setReadTimeout(1_000)
33.                .build();
34. 
35.        final OAuth20Service service = new ServiceBuilder()
36.                .apiKey(clientId)
37.                .apiSecret(clientSecret)
38.                .state(secretState)
39.                .callback("http://ift.tt/29ygMme")
40.                .asyncNingHttpClientConfig(clientConfig)
41.                .build(FacebookApi.instance());
42. 
43.        final Scanner in = new Scanner(System.in, "UTF-8");
44. 
45.        System.out.println("=== " + NETWORK_NAME + "'s Async OAuth Workflow ===");
46.        System.out.println();
47. 
48.        // Obtain the Authorization URL
49.        System.out.println("Fetching the Authorization URL...");
50.        final String authorizationUrl = service.getAuthorizationUrl();
51.        System.out.println("Got the Authorization URL!");
52.        System.out.println("Now go and authorize ScribeJava here:");
53.        System.out.println(authorizationUrl);
54.        System.out.println("And paste the authorization code here");
55.        System.out.print(">>");
56.        final String code = in.nextLine();
57.        System.out.println();
58. 
59.        System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
60.        System.out.print(">>");
61.        final String value = in.nextLine();
62.        if (secretState.equals(value)) {
63.            System.out.println("State value does match!");
64.        } else {
65.            System.out.println("Ooops, state value does not match!");
66.            System.out.println("Expected = " + secretState);
67.            System.out.println("Got      = " + value);
68.            System.out.println();
69.        }
70. 
71.        // Trade the Request Token and Verfier for the Access Token
72.        System.out.println("Trading the Request Token for an Access Token...");
73.        final OAuth2AccessToken accessToken = service.getAccessTokenAsync(code, null).get();
74.        System.out.println("Got the Access Token!");
75.        System.out.println("(if your curious it looks like this: " + accessToken
76.                + ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
77.        System.out.println();
78. 
79.        // Now let's go and ask for a protected resource!
80.        System.out.println("Now we're going to access a protected resource...");
81.        final OAuthRequestAsync request = new OAuthRequestAsync(Verb.GET, PROTECTED_RESOURCE_URL, service);
82.        service.signRequest(accessToken, request);
83.        final Response response = request.sendAsync(null).get();
84.        System.out.println("Got it! Lets see what we found...");
85.        System.out.println();
86.        System.out.println(response.getCode());
87.        System.out.println(response.getBody());
88. 
89.        System.out.println();
90.        System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
91.        service.closeAsyncClient();
92.    }
93.}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Meetup OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Meetup OAuth 1.0a using Scribe Java Meetup OAuth 1.0a Example using Scribe MeetupExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBu… Read More
  • Foursquare OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Foursquare OAuth 1.0a using Scribe Java Foursquare OAuth 1.0a Example using Scribe FoursquareExample.java import java.util.Scanner; import com.github.scribejava.core.build… Read More
  • Freelancer OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Freelancer OAuth 1.0a using Scribe Java Freelancer OAuth 1.0a Example using Scribe FreelancerExample.java import java.util.Scanner; import com.github.scribejava.core.build… Read More
  • Px500 OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Px500 OAuth 1.0a using Scribe Java Px500 OAuth 1.0a Example using Scribe Px500Example.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBuild… Read More
  • AWeber OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use AWeber OAuth 1.0a using Scribe Java AWeber OAuth 1.0a Example using Scribe AWeberExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBu… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Facebook OAuth 2.0 Example using Scribe Java with asyncNingHttpClientConfig Rating: 5 Reviewed By: eHowToNow