使用httr访问Fitbit日内时间序列:无效的redirect_uri

时间:2016-12-13 11:41:12

标签: httr fitbit

我尝试访问Fitbit日内时间序列数据。当我执行下面的代码时,我总是会收到以下错误:" 开发人员信息:invalid_request - 无效的redirect_uri参数值 "。

require(httr)

Sys.setenv("HTTR_SERVER_PORT" = "1410/")

fitbit.app <- oauth_app("[MyAppName}", key = "[MyClientID]", secret = "[MySecret]")

fitbit.ep <- oauth_endpoint(
  request = "https://api.fitbit.com/oauth2/token",
  authorize = "https://www.fitbit.com/oauth2/authorize",
  access = "https://api.fitbit.com/oauth2/token"
)

fitbit.token <- oauth2.0_token(fitbit.ep, 
                               fitbit.app, 
                               scope=c("heartrate"),
                               use_basic_auth = T)

我在https://dev.fitbit.com下配置了我的应用,以获得回调网址&#34; http://localhost:1410/&#34;。

此处描述了同样的问题:https://community.fitbit.com/t5/Web-API-Development/Having-trouble-with-redirect-uri-via-httr-in-R/m-p/1286028/highlight/false#M5194。但答案被标记为解决方案并不起作用。

另外,我想知道为什么&#34; response_type = code&#34;设置,而不是&#34; response_type = token&#34;正如我所料。我尝试设置额外参数 type =&#34; token&#34; ,但这并没有改变它。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

好的,幸运的是我自己发现了这个问题。答案也可能对其他人有用。

我将上述代码中的授权网址与Fitbit帮助页面生成的网址进行了比较:https://dev.fitbit.com/apps/oauthinteractivetutorial

这表明httr派生的redirect_uri连续两次包含后缀“%2F”。这意味着,编码的redirect_uri是“http://localhost:1410//”而不是“http://localhost:1410/”。解决方案是替换

Sys.setenv("HTTR_SERVER_PORT" = "1410/")

通过

Sys.setenv("HTTR_SERVER_PORT" = "1410")

在上面的代码中。当然,我找到了将环境变量设置为网络中某处的“1410 /”的代码片段。有人说它是让事情发挥作用的关键。这就是生活。

我希望这会有助于他人。这该死的fitbit API已经让我很头疼。