LoginSignup
6
5

More than 5 years have passed since last update.

[R] httrでQiitaのAPI v2を使いたかった

Last updated at Posted at 2015-07-26

追記:個人用アクセストークンは使えました。コメント欄参照。


使いたかったけど、oauth2.0_token()ではできませんでした。

ドキュメント

Applicationを登録

OAuth 2.0を使うためには、https://qiita.com/settings/applications からアプリケーションを登録する。「Authorization callback URL」はhttp://localhost:1410にする。

httrのコード

たぶんこんな感じだと思うんですよね。

library(httr)

qiita_endpoint <- oauth_endpoint(authorize = "/api/v2/oauth/authorize",
                                 access    = "/api/v2/access_tokens",
                                 base_url  = "https://qiita.com")

myapp <- oauth_app("qiita",
                   key = "(Client IDの値)",
                   secret = "(Client Secret)")

qiita_token <- oauth2.0_token(qiita_endpoint, myapp, scope = "read_qiita")

でも結果はエラーになります。。content typeって?

qiita_token$credentials
#> $message
#> [1] "Invalid content type"
#> 
#> $type
#> [1] "invalid_content_type"

RTFM

Qiitaのドキュメントを見ると、Content-Typeapplication/jsonにしないといけないみたいです。

POST /api/v2/access_tokens HTTP/1.1
Content-Type: application/json
Host: api.example.com

となっています。一方で、httrはどうやらapplication/x-www-form-urlencodedで送ってる雰囲気です。どっちが悪いのかよく分からないんですが、RFCではapplication/x-www-form-urlencodedになっています。ということで、httr側は変更しなさそう。

4.1.3. Access Token Request
The client makes a request to the token endpoint by sending the
following parameters using the "application/x-www-form-urlencoded"
format per Appendix B with a character encoding of UTF-8 in the HTTP
request entity-body:
(https://tools.ietf.org/html/rfc6749#section-4.1.3)

世の中的にはapplication/jsonでやってるサービスも結構あるんでしょうか? うーん、なにかいい解決方法あれば教えてください。。

6
5
3

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
6
5