メモ:httr::oauth2.0_token()のredirect_uriを変える

@teramonagiさんのこのパッケージ、httr::oauth2.0_token()だとムリなのかな?と思ってやってみたらムリだったけどメモ。

ナイーブにやるならこんな感じ?

library(httr)

app <- oauth_app("test",
                 key = "<自分のclient_id>",
                 secret = "<自分のclient_secret>")

endpoint <- oauth_endpoint(authorize = "https://www.healthplanet.jp/oauth/auth",
                           access = "https://www.healthplanet.jp/oauth/token")

oauth2.0_token(endpoint, app, scope = "innerscan")

と思ってやってみるとこんなエラーになります。

f:id:yutannihilation:20160321210331p:plain

なんでだろう、と思ったらこのAPIだとredirect_urilocalhost一択みたいで、httrが指定するhttp://localhost:PORTという形式は許されないみたいでした。(localhostの部分はHTTR_SERVERを、PORTの部分はHTTR_SERVER_PORTを、それぞれ環境変数で指定すれば変えることはできます。参考:httr/oauth-listener.r at f7593b7d5b3f9396469d1220dd4b38206fbb7d2e · hadley/httr · GitHub

そういえば@hoxo_m親分がよく魔改造で使ってるなーと思ってたunlockBinding()を使ってみます。

ns_httr <- asNamespace("httr")
unlockBinding("oauth_callback", env = ns_httr)
assign("oauth_callback", function() "localhost", envir = ns_httr)
lockBinding("oauth_callback", env = ns_httr)

これを使うと認証画面は突破できる。

けど、残念ながらこんなふうな温かみのある手作業を求められて先に進めません。

f:id:yutannihilation:20160321211315p:plain

うーん、むずい...