我试图使用http在R中进行GET调用,我不断获得授权401错误。R代码:
testfunction2 <- function()
{
set_config(verbose())
locus_url <- "https://api.locusenergy.com/v3/clients/5599"
r <- GET(url = "https://api.locusenergy.com/v3/clients/5599",
query=list(authorization="Bearer c935845d8fc1124757e66ce04d2c75d0"),
Accept="application/json")
}
结果:> print(testfunction2())
-> GET /v3/clients/5599
authorization=Bearer%20c935845d8fc1124757e66ce04d2c75d0 HTTP/1.1
-> User-Agent: libcurl/7.39.0 r-curl/0.9.1 httr/1.0.0
-> Host: api.locusenergy.com
-> Accept-Encoding: gzip, deflate
-> Cookie: AWSELB=D91FBFE1087EF6EBC125A126777051237474A8A060B6095B8E3C16151308453F8556B2A2E90CB2178F365FAA8AA8C29B124D15CA3EB859CFE615428E8D55C393ABB5B436BF
-> Accept: application/json, text/xml, application/xml, */*
->
<- HTTP/1.1 401 Unauthorized
<- Content-Type: application/json
<- Date: Sun, 16 Aug 2015 05:02:27 GMT
<- Server: Apache-Coyote/1.1
<- transfer-encoding: chunked
<- Connection: keep-alive
<-
我希望它返回一个200代码(而不是401,这意味着授权错误)
我知道令牌是正确的,因为如果我使用Postman (google插件)和Python,它就可以工作。这个令牌不能为你工作,因为我更改了它,因为我不能共享它。
Python代码: import http.client
conn = http.client.HTTPSConnection("api.locusenergy.com")
headers = {
'authorization': "Bearer 935845d8fc1124757e66ce04d2c75d0"
}
conn.request("GET", "/v3/clients/5599", headers=headers)
res = conn.getresponse()
data = res.read()
print(data)
结果来自Python
b'{"statusCode":200,"partnerId":4202,"tz":"US/Arizona","firstName":"xxx","lastName":"xxxx","email":"xxxx@aol.com","id":5599}'
所以,问题是我在R中做错了什么,或者你能给我一些提示吗?这将无法为您复制,因为令牌过期,我无法共享它。会不会是授权中的空格?授权="无记名935845 d8fc1124757e66ce04d2c75d0"?在R中get调用的详细输出中是否有任何提示?作为参考,这是该网站的API页面:https://developer.locusenergy.com/
站点需要OAUTH2身份验证才能返回令牌。我没有包含该代码,但我验证了令牌可以与Python和Postman一起使用。
现在您正在传递httr
代码的查询字符串中的授权值,而不是像您在python代码中所做的那样在http头中。而不是使用
GET(url = "https://api.locusenergy.com/v3/clients/5599",
accept_json(),
add_headers(Authorization="Bearer c935845d8fc1124757e66ce04d2c75d0")
)