CURL中的多重身份验证

时间:2019-07-23 07:05:19

标签: r

上下文

我想从R访问API并具有正确的凭据。

问题在于,身份验证需要传递三个参数(用户名,密码和组织)。

我不熟悉来自R的API调用,并且我不知道如何传递此“组织”参数。

示例

请求令牌的API调用示例如下(摘自文档):

curl -X POST "https://URL.com" \
-H "Content-Type: application/json" \
--data "{\"user_name\": \"<username>\" \
\"password\": \"<mypassword>\",\
\"organisation\": \"<organisation>\"}"

我使用crul包尝试过:

require("crul")

z <- crul::HttpClient$new(url = "https://THEURL.com", 
    auth(user = "myuser", 
         pwd = "mypwd"))
z$get()

哪个返回:

<crul response>
url: https://THEURL.com
request_headers: 
User-Agent: libcurl/7.59.0 r-curl/3.3 crul/0.8.0
Accept-Encoding: gzip, deflate
Accept: application/json, text/xml, application/xml, */*
response_headers: 
status: HTTP/1.1 403 Forbidden
content-type: application/json
content-length: 211
connection: keep-alive
x-amzn-errortype: IncompleteSignatureException

我假设x-amzn-errortype是指缺少的organisation身份验证变量。

问题

如何将变量“组织”传递给函数?

或者有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

我认为当您处理HTTP请求时,使用包httr会更容易。我认为您的示例请求可能是:

library(httr)

POST("https://THEURL.com",
     body = list(user_name = "<username>", password = "<mypassword>", organisation = "<organisation>"),
     encode = "json")