In this tutorial we will see an example on how to use ScribeJava with Twitter (OAuth 1.0a).
please use always the latest version
a callback(“http://your_callback_url”) call before build()
For this you need to redirect them to the following URL:
We will see the more example programs in up coming tutorials.
Step 1: Install ScribeJava
First of all, you need to install ScribeJava. You either download two jars manually from the downloads page and http://ift.tt/29ygJHb and include apache commons codec, or just let maven take care of everything adding this to your pom.xml file:please use always the latest version
<dependency> <groupId>com.github.scribejava</groupId> <artifactId>scribejava-apis</artifactId> <version>2.8.1</version> </dependency>Or for gradle:
dependencies { compile 'com.github.scribejava:scribejava-apis:2.8.1' }
Step 2: Create the OAuthService object
final OAuth10aService service = new ServiceBuilder() .apiKey("your_api_key") .apiSecret("your_api_secret") .build(TwitterApi.instance());The example uses OOB OAuth, if you want to pass a callbackUrl so that Twitter redirects you there just add
a callback(“http://your_callback_url”) call before build()
Step 3: Get the request token
final OAuth1RequestToken requestToken = service.getRequestToken();Step 4: Making the user validate your request token
Let’s help your users authorize your app to do the OAuth calls.For this you need to redirect them to the following URL:
String authUrl = service.getAuthorizationUrl(requestToken);After this either the user will get a verifier code (if this is an OOB request) or you’ll receive a redirect from Twitter with the verifier and the requestToken on it (if you provided a callbackUrl)
Step 5: Get the access Token
Now that you have (somehow) the verifier, you need to exchange your requestToken and verifier for an accessToken which is the one used to sign requests.final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, "verifier you got from the user/callback");
Step 6: Sign request
You are all set to make your first API call, so let’s do it!final OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json", service); service.signRequest(accessToken, request); // the access token from step 4 final Response response = request.send(); System.out.println(response.getBody());That’s it!
We will see the more example programs in up coming tutorials.
0 comments:
Post a Comment