In this example we are going to see about how to use TutBy OAuth2.0 using Scribe Java
TutBy OAuth2.0 Example using Scribe
TutByExample.java
01.
import
java.util.Scanner;
02.
03.
import
com.github.scribejava.core.builder.ServiceBuilder;
04.
import
com.github.scribejava.core.model.OAuthRequest;
05.
import
com.github.scribejava.core.model.Response;
06.
import
com.github.scribejava.core.model.Verb;
07.
08.
import
com.github.scribejava.apis.TutByApi;
09.
import
com.github.scribejava.core.model.OAuth2AccessToken;
10.
import
com.github.scribejava.core.oauth.OAuth20Service;
11.
import
java.io.IOException;
12.
13.
public
abstract
class
TutByExample {
14.
15.
private
static
final
String NETWORK_NAME =
"Tut.by"
;
16.
private
static
final
String PROTECTED_RESOURCE_URL =
"http://ift.tt/29MDIkk"
;
17.
18.
public
static
void
main(String... args)
throws
IOException {
19.
// Replace these with your client id and secret
20.
final
String clientId =
"your client id"
;
21.
final
String clientSecret =
"your client secret"
;
22.
final
OAuth20Service service =
new
ServiceBuilder()
23.
.apiKey(clientId)
24.
.apiSecret(clientSecret)
25.
.callback(
"http://ift.tt/29ygMme"
)
26.
.build(TutByApi.instance());
27.
final
Scanner in =
new
Scanner(System.in,
"UTF-8"
);
28.
29.
System.out.println(
"=== "
+ NETWORK_NAME +
"'s OAuth Workflow ==="
);
30.
System.out.println();
31.
32.
System.out.println(
"Fetching the Authorization URL..."
);
33.
final
String authorizationUrl = service.getAuthorizationUrl();
34.
System.out.println(
"Got the Authorization URL!"
);
35.
System.out.println(
"Now go and authorize ScribeJava here:"
);
36.
System.out.println(authorizationUrl);
37.
System.out.println(
"And paste the authorization code here"
);
38.
System.out.print(
">>"
);
39.
final
String code = in.nextLine();
40.
System.out.println();
41.
42.
// Trade the Request Token and Verfier for the Access Token
43.
System.out.println(
"Trading the Request Token for an Access Token..."
);
44.
final
OAuth2AccessToken accessToken = service.getAccessToken(code);
45.
System.out.println(
"Got the Access Token!"
);
46.
System.out.println(
"(if your curious it looks like this: "
+ accessToken
47.
+
", 'rawResponse'='"
+ accessToken.getRawResponse() +
"')"
);
48.
System.out.println();
49.
50.
// Now let's go and ask for a protected resource!
51.
System.out.println(
"Now we're going to access a protected resource..."
);
52.
final
OAuthRequest request =
new
OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL, service);
53.
service.signRequest(accessToken, request);
54.
final
Response response = request.send();
55.
System.out.println(
"Got it! Lets see what we found..."
);
56.
System.out.println();
57.
System.out.println(response.getCode());
58.
System.out.println(response.getBody());
59.
60.
System.out.println();
61.
System.out.println(
"Thats it man! Go and build something awesome with ScribeJava! :)"
);
62.
}
63.
}
0 comments:
Post a Comment