首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Password Grant在R OAuth2中使用HTTR包生成令牌

Password Grant在R OAuth2中使用HTTR包生成令牌
EN

Stack Overflow用户
提问于 2021-06-23 11:02:12
回答 1查看 30关注 0票数 1

这对API来说是全新的,对我来说有点不知所措。读了一点书,到处找了找,但还是很困惑。我得到了以下代码,以便在curl中运行,以使用oauth2和授予密码的方式生成令牌。我想用R实现这段代码,但会遇到一些问题。

代码语言:javascript
运行
复制
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中成功使用了该令牌:

代码语言:javascript
运行
复制
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中生成令牌,而不是像我以前那样使用另一个程序。这就是我的错误所在。我认为基本的格式是:

代码语言:javascript
运行
复制
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
)
EN

回答 1

Stack Overflow用户

发布于 2021-06-23 12:23:11

根据MrFlick先生上面的评论,我们可以使用httr包将curl auhtorization命令集成到POST中。然后,我们查看此函数返回的对象的内容,该对象将在列表的第一项中包含访问令牌。

代码语言:javascript
运行
复制
token <- content(httr::POST("https://{website}.com/rest/v2/oauth/token", body=list(grant_type="password", "username="{username}", password="{password}"), encode="form"))[[1]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68093017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档