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

# やりたいこと
* JAX-RSでyouRoom APIにOAtuthでアクセスする

# 前提条件
* RIにはJersey 2.xを使用
* OAuth1を使用
* youRoom APIは[こちら](http://apidoc.youroom.in/)
* Cosumer Key/Consumer Secretは取得済み

# 実装してみる

* お試しで[User/verify_credentials](http://apidoc.youroom.in/rest-user-verify-credentials)を呼び出してみる

## 依存関係の解決

* 下記のように依存性を定義する(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コード取得しての認証

```java: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が取得済みの場合

```java: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そのまま試した感じです）

# 関連した投稿
* [JAX-RS Client メモ - REST API を呼び出してレスポンスをBeanにマッピングする](http://qiita.com/pekokun@github/items/ac4106c01a126750eee3)