In this example we are going to see about how to use StackExchange OAuth 2.0 using Scribe Java
StackExchange OAuth 2.0 Example using Scribe
StackExchangeExample.java
01.
import
java.util.Random;
02.
import
java.util.Scanner;
03.
04.
import
com.github.scribejava.apis.StackExchangeApi;
05.
import
com.github.scribejava.core.builder.ServiceBuilder;
06.
import
com.github.scribejava.core.model.OAuth2AccessToken;
07.
import
com.github.scribejava.core.model.OAuthRequest;
08.
import
com.github.scribejava.core.model.Response;
09.
import
com.github.scribejava.core.model.Verb;
10.
import
com.github.scribejava.core.oauth.OAuth20Service;
11.
import
java.io.IOException;
12.
13.
public
abstract
class
StackExchangeExample {
14.
15.
private
static
final
String NETWORK_NAME =
"Stack Exchange"
;
16.
private
static
final
String PROTECTED_RESOURCE_URL =
"http://ift.tt/2ae8tKC"
;
17.
18.
public
static
void
main(String... args)
throws
IOException {
19.
// Replace these with your client id, secret, application key and
20.
// optionally site name
21.
final
String clientId =
"your client id"
;
22.
final
String clientSecret =
"your client secret"
;
23.
final
String key =
"your client key"
;
24.
// Enter one of Stack Exchange site names the user has account with.
25.
final
String site =
"stackoverflow"
;
26.
final
String secretState =
"secret"
+
new
Random().nextInt(999_999);
27.
final
OAuth20Service service =
new
ServiceBuilder()
28.
.apiKey(clientId)
29.
.apiSecret(clientSecret)
30.
.state(secretState)
31.
.callback(
"http://ift.tt/29ygMme"
)
32.
.build(StackExchangeApi.instance());
33.
final
Scanner in =
new
Scanner(System.in,
"UTF-8"
);
34.
35.
System.out.println(
"=== "
+ NETWORK_NAME +
"'s OAuth Workflow ==="
);
36.
System.out.println();
37.
38.
// Obtain the Authorization URL
39.
System.out.println(
"Fetching the Authorization URL..."
);
40.
final
String authorizationUrl = service.getAuthorizationUrl();
41.
System.out.println(
"Got the Authorization URL!"
);
42.
System.out.println(
"Now go and authorize ScribeJava here:"
);
43.
System.out.println(authorizationUrl);
44.
System.out.println(
"And paste the authorization code here"
);
45.
System.out.print(
">>"
);
46.
final
String code = in.nextLine();
47.
System.out.println();
48.
49.
System.out.println(
"And paste the state from server here. We have set 'secretState'='"
+ secretState +
"'."
);
50.
System.out.print(
">>"
);
51.
final
String value = in.nextLine();
52.
if
(secretState.equals(value)) {
53.
System.out.println(
"State value does match!"
);
54.
}
else
{
55.
System.out.println(
"Ooops, state value does not match!"
);
56.
System.out.println(
"Expected = "
+ secretState);
57.
System.out.println(
"Got = "
+ value);
58.
System.out.println();
59.
}
60.
61.
// Trade the Request Token and Verfier for the Access Token
62.
System.out.println(
"Trading the Request Token for an Access Token..."
);
63.
final
OAuth2AccessToken accessToken = service.getAccessToken(code);
64.
System.out.println(
"Got the Access Token!"
);
65.
System.out.println(
"(if your curious it looks like this: "
+ accessToken
66.
+
", 'rawResponse'='"
+ accessToken.getRawResponse() +
"')"
);
67.
System.out.println();
68.
69.
// Now let's go and ask for a protected resource!
70.
System.out.println(
"Now we're going to access a protected resource..."
);
71.
final
OAuthRequest request =
new
OAuthRequest(Verb.GET,
72.
PROTECTED_RESOURCE_URL +
"?site="
+ site +
"&key="
+ key, service);
73.
service.signRequest(accessToken, request);
74.
final
Response response = request.send();
75.
System.out.println(
"Got it! Lets see what we found..."
);
76.
System.out.println();
77.
System.out.println(response.getCode());
78.
System.out.println(response.getBody());
79.
80.
System.out.println();
81.
System.out.println(
"Thats it man! Go and build something awesome with ScribeJava! :)"
);
82.
83.
}
84.
}
0 comments:
Post a Comment