Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Jonas Kress edited this pageOct 17, 2019 ·45 revisions

Getting Started With ScribeJava (AKA ‘OAuth made so easy your grandma can do it’) for the OAuth 1.0a

In this tutorial we will walk through an example on how to use ScribeJava with Twitter (OAuth 1.0a).

For an executable example please gohere

Quick-run an example

1. Install maven
2. Replace sample keys and tokens with your real keys-tokens in e.g. `Vimeo20Example.java`
3. Compile by

mvn compiler:compile

4. Run the example by
mvn exec:java -Dexec.mainClass=“org.scribe.examples.Vimeo20Example” -Dexec.classpathScope=test test-compile exec:java

Step Zero: Install ScribeJava

First of all, you need to install ScribeJava. You either download two jars manually from thedownloads page and http://mvnrepository.com/artifact/com.github.scribejava/scribejava-core and includeapache commons codec, or just let maven take care of everything adding this to your pom.xml file:

<dependency>  <groupId>com.github.scribejava</groupId>  <artifactId>scribejava-apis</artifactId>  <version>6.4.1</version> // please use always the latest version</dependency>

Or for gradle:

dependencies {    implementation'com.github.scribejava:scribejava-apis:6.4.1'}

Step One: Create the OAuthService object

finalOAuth10aServiceservice =newServiceBuilder("your_api_key")                           .apiSecret("your_api_secret")                           .build(TwitterApi.instance());

Yup, that’s it. :)

The example usesOOB OAuth, if you want to pass a callbackUrl so that Twitter redirects you there just add
acallback("http://your_callback_url") call beforebuild()

Step Two: Get the request token

finalOAuth1RequestTokenrequestToken =service.getRequestToken();

Easy right?

Step Three: 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 followingURL:

StringauthUrl =service.getAuthorizationUrl(requestToken);

After this either the user will get a verifier code (if this is anOOB request) or you’ll receive a redirect from Twitter with the verifier and the requestToken on it (if you provided a callbackUrl)

Step Four: 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.

finalOAuth1AccessTokenaccessToken =service.getAccessToken(requestToken,"verifier you got from the user/callback");

Step Five: Sign request

You are all set to make your firstAPI call, so let’s do it!

finalOAuthRequestrequest =newOAuthRequest(Verb.GET,"https://api.twitter.com/1.1/account/verify_credentials.json");service.signRequest(accessToken,request);// the access token from step 4finalResponseresponse =service.execute(request);System.out.println(response.getBody());

That’s it! You already know everything you need to start building a cool OAuth application.

Good Luck!

Doubts – Comments

email me at:

https://github.com/scribejava/scribejava/blob/master/pom.xml#L44

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp