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