88import com .github .scribejava .apis .FacebookApi ;
99import com .github .scribejava .core .builder .ServiceBuilder ;
1010import com .github .scribejava .core .model .OAuth2AccessToken ;
11- import com .github .scribejava .core .model .OAuthRequestAsync ;
11+ import com .github .scribejava .core .model .OAuthRequest ;
1212import com .github .scribejava .core .model .Response ;
1313import com .github .scribejava .core .model .Verb ;
1414import com .github .scribejava .core .oauth .OAuth20Service ;
@@ -35,62 +35,62 @@ public static void main(String... args) throws InterruptedException, ExecutionEx
3535 .setReadTimeout (1_000 )
3636 .build ());
3737
38- final OAuth20Service service = new ServiceBuilder ()
38+ try ( OAuth20Service service = new ServiceBuilder ()
3939 .apiKey (clientId )
4040 .apiSecret (clientSecret )
4141 .state (secretState )
4242 .callback ("http://www.example.com/oauth_callback/" )
4343 .httpClientConfig (clientConfig )
44- .build (FacebookApi .instance ());
44+ .build (FacebookApi .instance ())) {
45+ final Scanner in = new Scanner (System .in , "UTF-8" );
4546
46- final Scanner in = new Scanner (System .in , "UTF-8" );
47+ System .out .println ("=== " + NETWORK_NAME + "'s Async OAuth Workflow ===" );
48+ System .out .println ();
4749
48- System .out .println ("=== " + NETWORK_NAME + "'s Async OAuth Workflow ===" );
49- System .out .println ();
50+ // Obtain the Authorization URL
51+ System .out .println ("Fetching the Authorization URL..." );
52+ final String authorizationUrl = service .getAuthorizationUrl ();
53+ System .out .println ("Got the Authorization URL!" );
54+ System .out .println ("Now go and authorize ScribeJava here:" );
55+ System .out .println (authorizationUrl );
56+ System .out .println ("And paste the authorization code here" );
57+ System .out .print (">>" );
58+ final String code = in .nextLine ();
59+ System .out .println ();
5060
51- // Obtain the Authorization URL
52- System .out .println ("Fetching the Authorization URL..." );
53- final String authorizationUrl = service .getAuthorizationUrl ();
54- System .out .println ("Got the Authorization URL!" );
55- System .out .println ("Now go and authorize ScribeJava here:" );
56- System .out .println (authorizationUrl );
57- System .out .println ("And paste the authorization code here" );
58- System .out .print (">>" );
59- final String code = in .nextLine ();
60- System .out .println ();
61+ System .out .println ("And paste the state from server here. We have set 'secretState'='"
62+ + secretState + "'." );
63+ System .out .print (">>" );
64+ final String value = in .nextLine ();
65+ if (secretState .equals (value )) {
66+ System .out .println ("State value does match!" );
67+ } else {
68+ System .out .println ("Ooops, state value does not match!" );
69+ System .out .println ("Expected = " + secretState );
70+ System .out .println ("Got = " + value );
71+ System .out .println ();
72+ }
6173
62- System .out .println ("And paste the state from server here. We have set 'secretState'='" + secretState + "'." );
63- System .out .print (">>" );
64- final String value = in .nextLine ();
65- if (secretState .equals (value )) {
66- System .out .println ("State value does match!" );
67- } else {
68- System .out .println ("Ooops, state value does not match!" );
69- System .out .println ("Expected = " + secretState );
70- System .out .println ("Got = " + value );
74+ // Trade the Request Token and Verfier for the Access Token
75+ System .out .println ("Trading the Request Token for an Access Token..." );
76+ final OAuth2AccessToken accessToken = service .getAccessTokenAsync (code , null ).get ();
77+ System .out .println ("Got the Access Token!" );
78+ System .out .println ("(if your curious it looks like this: " + accessToken
79+ + ", 'rawResponse'='" + accessToken .getRawResponse () + "')" );
7180 System .out .println ();
72- }
73-
74- // Trade the Request Token and Verfier for the Access Token
75- System .out .println ("Trading the Request Token for an Access Token..." );
76- final OAuth2AccessToken accessToken = service .getAccessTokenAsync (code , null ).get ();
77- System .out .println ("Got the Access Token!" );
78- System .out .println ("(if your curious it looks like this: " + accessToken
79- + ", 'rawResponse'='" + accessToken .getRawResponse () + "')" );
80- System .out .println ();
8181
82- // Now let's go and ask for a protected resource!
83- System .out .println ("Now we're going to access a protected resource..." );
84- final OAuthRequestAsync request = new OAuthRequestAsync (Verb .GET , PROTECTED_RESOURCE_URL );
85- service .signRequest (accessToken , request );
86- final Response response = service .execute (request , null ).get ();
87- System .out .println ("Got it! Lets see what we found..." );
88- System .out .println ();
89- System .out .println (response .getCode ());
90- System .out .println (response .getBody ());
82+ // Now let's go and ask for a protected resource!
83+ System .out .println ("Now we're going to access a protected resource..." );
84+ final OAuthRequest request = new OAuthRequest (Verb .GET , PROTECTED_RESOURCE_URL );
85+ service .signRequest (accessToken , request );
86+ final Response response = service .execute (request , null ).get ();
87+ System .out .println ("Got it! Lets see what we found..." );
88+ System .out .println ();
89+ System .out .println (response .getCode ());
90+ System .out .println (response .getBody ());
9191
92- System .out .println ();
93- System .out .println ("Thats it man! Go and build something awesome with ScribeJava! :)" );
94- service . closeAsyncClient ();
92+ System .out .println ();
93+ System .out .println ("Thats it man! Go and build something awesome with ScribeJava! :)" );
94+ }
9595 }
9696}
0 commit comments