简体   繁体   English

如何使用 R 中的 oauth2 令牌从 Web 服务器检索数据?

[英]how to retrieve data from a web server using an oauth2 token in R?

I've successfully received an access token from an oauth2.0 request so that I can start obtaining some data from the server.我已成功收到来自 oauth2.0 请求的访问令牌,以便我可以开始从服务器获取一些数据。 However, I keep getting error 403 on each attempt.但是,每次尝试时我都会收到错误 403。 APIs are very new to me and I only am entry level in using R so I can't figure out whats wrong with my request. API 对我来说很新,我只是使用 R 的入门级,所以我无法弄清楚我的请求有什么问题。 I'm using the crul package currently, but I've tried to make the request with the httr package as well, but I can't get anything through without encountering the 403 error.我目前正在使用 crul 包,但我也尝试使用 httr 包发出请求,但是如果没有遇到 403 错误,我无法通过任何东西。 I have a shiny app which in the end I'd like to be able to refresh with data imported from this other application which actually stores data, but I want to try to pull data to my console locally first so I can understand the basic process of doing so.我有一个闪亮的应用程序,最后我希望能够使用从实际存储数据的其他应用程序导入的数据进行刷新,但我想先尝试将数据在本地拉到我的控制台,以便我了解基本过程这样做。 I will post some of my current attempts.我将发布一些我目前的尝试。

(x <- HttpClient$new(
url = 'https://us.castoredc.com',
opts = list( exceptions = FALSE),
headers = list())
)
res.token <- x$post('oauth/token',
body = list(client_id = "{id}",
client_secret = "{secret}",
grant_type = 'client_credentials'))

importantStuff <- jsonlite::fromJSON(res$parse("UTF-8"))

token <- paste("Bearer", importantStuff$access_token)

I obtain my token, but the following doesn't seem to work.### I'm attempting to get the list of study codes so that I can call on them in further requests to actually get data from a study.我获得了我的令牌,但以下内容似乎不起作用。### 我正在尝试获取研究代码列表,以便我可以在进一步请求中调用它们以实际从研究中获取数据。

res.studies <- x$get('/api/study',headers = list(Authorization = 
token,client_id = "{id}",
client_secret = "{secret}",
grant_type = 'client_credentials'),
body = list(
content_type = 'application/json'))

Their support team gave me the above endpoint to access the content, but I get 403 so I think i'm not using my token correctly?他们的支持团队给了我上面的端点来访问内容,但我得到了 403,所以我认为我没有正确使用我的令牌?

status: 403
access-control-allow-headers: Authorization
access-control-allow-methods: Get,Post,Options,Patch

I'm the CEO at Castor EDC and although its pretty cool to see a Castor EDC question on here, I apologize for the time you lost over trying to figure this out.我是 Castor EDC 的 CEO,虽然在这里看到 Castor EDC 的问题很酷,但我为你在试图解决这个问题上浪费的时间道歉。 Was our support team not able to provide more assistance?我们的支持团队是否无法提供更多帮助?

Regardless, I have actually used our API quite a bit in R and we also have an amazing R Engineer in house if you need more help.无论如何,我实际上已经在 R 中大量使用了我们的 API,如果您需要更多帮助,我们内部还有一位出色的 R 工程师。

Reflecting on your answer, yes, you always need a Study ID to be able to do anything interesting with the API.回想一下你的回答,是的,你总是需要一个 Study ID 才能使用 API 做任何有趣的事情。 One thing that could make your life A LOT easier is our R API wrapper, you can find that here: https://github.com/castoredc/castoRedc可以让您的生活更轻松的一件事是我们的 R API 包装器,您可以在这里找到: https : //github.com/castoredc/castoRedc

With that you would:有了它,你会:

remotes::install_github("castoredc/castoRedc")
library(castoRedc)
castor_api <- CastorData$new(key = Sys.getenv("CASTOR_KEY"), 
                             secret = Sys.getenv("CASTOR_SECRET"), 
                             base_url = "https://data.castoredc.com")
example_study_id <- studies[["study_id"]][1]
fields <- castor_api$getFields(example_study_id)

etc.等等。

Hope that makes you life a lot easier in the future.希望这能让你在未来的生活更轻松。

So, After some investigation, It turns out that you first have to make a request to obtain another id for each Castor study under your username.因此,经过一些调查,事实证明您首先必须请求为您的用户名下的每个 Castor 研究获取另一个 ID。 I will post some example code that worked finally.我将发布一些最终有效的示例代码。

req.studyinfo <- httr::GET(url = "us.castoredc.com/api/study"
,httr::add_headers(Authorization = token))
json <- httr::content(req.studyinfo,as = "text")
studies <- fromJSON(json)

Then, this will give you a list of your studies in Castor for which you can obtain the ID that you care about for your endpoints.然后,这将为您提供您在 Castor 中的学习列表,您可以从中获取您关心的端点 ID。 It will be a list that contains a data frame containing this information.它将是一个包含包含此信息的数据框的列表。 you use the same format with whatever endpoint you like that is posted in their documentation to retrieve data.您可以使用与您喜欢的任何端点相同的格式,这些端点发布在他们的文档中以检索数据。 Thank you for your observations!感谢您的关注! I will leave this here in case anyone is employed to develop anything from data used in the Castor EDC.如果有人受雇从 Castor EDC 中使用的数据开发任何内容,我将把它留在这里。 Their documentation was vague to me, so maybe it will help someone in the future.他们的文档对我来说很模糊,所以也许将来会对某人有所帮助。 Example for next step:下一步示例:

req.studydata <- httr::GET("us.castoredc.com/api/study/{study id obtained 
from previous step}/data-point- 
collection/study",,httr::add_headers(Authorization = 
token))
json.data <- httr::content(req.studydata,as = "text")
data <- fromJSON(json.data)

This worked for me, I removed the Sys.getenv() part这对我有用,我删除了Sys.getenv()部分

library(castoRedc)
castor_api <- CastorData$new(key = "CASTOR_KEY", 
                             secret = "CASTOR_SECRET", 
                             base_url = "https://data.castoredc.com")
example_study_id <- studies[["study_id"]][1]
fields <- castor_api$getFields(example_study_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM