使用httr在R中进行OAuth2身份验证

4
我正在尝试从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
                        )

代码的最后一部分(token)产生了以下错误:
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

遇到了同样的问题。向支持团队发送了一条消息,他们回复说要确保包含头部信息 "Content-Type: application/x-www-form-urlencoded",并且将请求体作为纯文本/原始文本发送,并包含 "grant_type=authorization_code&code=<AUTHORIZATION_CODE>"。

我还没有测试过这个方法,但希望它能帮助你解决问题,如果你还没有找到解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接