使用httr与R进行OAuth2身份验证

时间:2020-01-29 17:10:52

标签: r oauth-2.0 httr

我正在尝试从Polar Accesslink API读取一些数据。我已经阅读了文档和示例(我不太熟悉Python)以及Oauth2.0的httr示例/演示:

https://www.polar.com/accesslink-api/#polar-accesslink-api

https://github.com/polarofficial/accesslink-example-python

这是我的代码:

library(httr)

client_id <- rstudioapi::askForPassword()

client_secret <- rstudioapi::askForPassword()

# redirect_uri determined in registration to http://localhost:1410/

app <- oauth_app(appname = "polar_app",
                 key = client_id,
                 secret = client_secret)

endpoint <- oauth_endpoint(
  request = NULL,
  authorize = "https://flow.polar.com/oauth2/authorization",
  access = "https://polarremote.com/v2/oauth2/token")


token <- oauth2.0_token(endpoint = endpoint,
                        app = app,
                        scope = "accesslink.read_all", # tested also without scope
                        use_basic_auth = T # tested both T and F
                        )

代码的最后一部分(令牌)产生以下错误:

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params,  : 
  Bad Request (HTTP 400). Failed to get an access token.

它使用URL localhost:1410 /?state = {STATE}&code = {CODE}打开浏览器,并说:

“身份验证已完成。请关闭此页面并返回R。”

基于API文档和github Python示例,我不知道是否需要为oauth2.0_token中的user_params或query_authorize_extra设置一些值。

1 个答案:

答案 0 :(得分:0)

有同样的问题。发送了一条消息以寻求支持,他们回来时确保您包含标题“ Content-Type:application / x-www-form-urlencoded”,并且正文以纯文本/原始文本发送并包含“ grant_type = authorization_code&code = ”。

我还没有测试过,但是希望它可以帮助您,如果您尚未找到解决方案