追記:個人用アクセストークンは使えました。コメント欄参照。
使いたかったけど、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-Type
はapplication/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
でやってるサービスも結構あるんでしょうか? うーん、なにかいい解決方法あれば教えてください。。