【问题标题】:OAuth2 authentication with R using httr使用 httr 使用 R 进行 OAuth2 身份验证
【发布时间】:2020-01-29 17:10:52
【问题描述】:

我正在尝试从 Polar Accesslink API 读取一些数据。我已经阅读了 Oauth2.0 的文档和示例(我对 Python 不太熟悉)以及 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 设置一些值。

【问题讨论】:

    标签: r oauth-2.0 httr


    【解决方案1】:

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

    我没有对此进行测试,但如果您还没有找到解决方案,希望它对您有所帮助

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2023-03-12
      • 1970-01-01
      • 2021-11-17
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多