5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAX-RS Client メモ - OAuth(OAuth1)をやってみる

Posted at

やりたいこと

  • JAX-RSでyouRoom APIにOAtuthでアクセスする

前提条件

  • RIにはJersey 2.xを使用
  • OAuth1を使用
  • youRoom APIはこちら
  • Cosumer Key/Consumer Secretは取得済み

実装してみる

依存関係の解決

  • 下記のように依存性を定義する(Gradleの場合)
build.gradle
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.+'

// これがOAuth1用
compile group: 'org.glassfish.jersey.security', name: 'oauth1-client', version: '2.+'

PINコード取得しての認証

Demo.java
public class Demo {

    private static final String CONSUMER_KEY = "*****";
    private static final String CONSUMER_SECRET = "*****";

    private static final BufferedReader IN = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) {
        ConsumerCredentials consumerCredentials = new ConsumerCredentials(CONSUMER_KEY, CONSUMER_SECRET);

        OAuth1AuthorizationFlow authFlow = OAuth1ClientSupport.builder(consumerCredentials)
                .authorizationFlow(
                    "https://www.youroom.in/oauth/request_token",
                    "https://www.youroom.in/oauth/access_token",
                    "https://www.youroom.in/oauth/authorize")
                .build();

        String authorizationUri = authFlow.start();
        System.out.println("Enter the following URI into a web browser and authorize me:");
        System.out.println(authorizationUri);
        System.out.print("Enter the authorization code: ");
        String verifier = null;
        try {
            verifier = IN.readLine();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        AccessToken accessTokne = authFlow.finish(verifier);
        Feature filterFeature = authFlow.getOAuth1Feature();

        Client client = ClientBuilder.newBuilder()
                .register(filterFeature)
                .build();

        String response = client.target("https://www.youroom.in/verify_credentials").request(MediaType.APPLICATION_JSON).get(String.class);

        System.out.println(response);
    }
}
  • System.out.println(authorizationUri);で表示されたURLにブラウザ等でアクセスし、認証してPINを取得します。
  • 取得したPINを標準入力に入力します。

Access TokenおよびAccess Token Secreetが取得済みの場合

Demo.java
public class Demo {

    private static final String CONSUMER_KEY = "*****";
    private static final String CONSUMER_SECRET = "*****";

    private static final String ACCESS_TOKEN = "*****";
    private static final String ACCESS_TOKEN_SECRET = "*****";

    public static void main(String[] args) {
        ConsumerCredentials consumerCredentials = new ConsumerCredentials(CONSUMER_KEY, CONSUMER_SECRET);
        AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

        Feature feature = OAuth1ClientSupport.builder(consumerCredentials).feature().accessToken(accessToken).build();

        Client client = ClientBuilder.newBuilder()
                .register(feature)
                .build();

        String response = client.target("https://www.youroom.in/verify_credentials").request(MediaType.APPLICATION_JSON).get(String.class);

        System.out.println(response);
    }
}

参考

https://github.com/jersey/jersey/tree/2.11/examples/oauth-client-twitter
(このexampleそのまま試した感じです)

関連した投稿

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?