这对API来说是全新的,对我来说有点不知所措。读了一点书,到处找了找,但还是很困惑。我得到了以下代码,以便在curl中运行,以使用oauth2和授予密码的方式生成令牌。我想用R实现这段代码,但会遇到一些问题。
curl -H "Content-type: application/x-www-form-urlencoded" -H "Authorization: Basic xxxyyy123xxx==" -d "grant_type=password&username={username}&password={password}" https://{website}.com/rest/v2/oauth/token
当我在终端中运行上面的命令时,它会生成一个令牌,我已经使用以下代码在R中成功使用了该令牌:
token <- {character string generated by the above curl command}
call <- "https://{website}.com/rest/v2/services/APIService/getRegistryRecordByAccessionID?id={id#}"
df <- as.data.frame(fromJSON(content(httr::GET(call, add_headers(Authorization = paste("Bearer", token))), "text"), flatten = TRUE))
我想要的是在R中生成令牌,而不是像我以前那样使用另一个程序。这就是我的错误所在。我认为基本的格式是:
token <- oauth2.0_token(endpoint = oauth_endpoint(
authorize = NULL,
access = "https://{website}.com/rest/v2/oauth/token"),
app = oauth_app(), #not sure how to set this up
use_basic_auth = T,
add_headers(Content-type = "application/x-www-form-urlencoded",
Authorization = "Basic xxxyyy123xxx==") #not sure if i need the authorization header here, I believe I need the content-type heder
)
发布于 2021-06-23 12:23:11
根据MrFlick先生上面的评论,我们可以使用httr包将curl auhtorization命令集成到POST中。然后,我们查看此函数返回的对象的内容,该对象将在列表的第一项中包含访问令牌。
token <- content(httr::POST("https://{website}.com/rest/v2/oauth/token", body=list(grant_type="password", "username="{username}", password="{password}"), encode="form"))[[1]]
https://stackoverflow.com/questions/68093017
复制相似问题