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