【发布时间】:2020-06-21 12:20:59
【问题描述】:
我正在尝试从 REST API 访问 JSON 数据(在 R 中)。
为了验证自己,我需要在https://dashboard.server.eu/login 中使用POST 方法。需要发送的数据是邮箱和密码:
library(httr)
login <- list(
email = "my@email.com",
password = "mypass"
)
res <- POST("https://dashboard.server.eu/login", body = login, encode = "form", verbose())
执行上述操作时,我得到以下输出:
-> POST /login HTTP/1.1
-> Host: dashboard.server.eu
-> User-Agent: libcurl/7.59.0 r-curl/3.3 httr/1.4.1
-> Accept-Encoding: gzip, deflate
-> Cookie: session=10kq9qv1udf0107F4C70RY14fsum41sq50
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Content-Length: 53
->
>> email=my%40email.com&password=mypass
<- HTTP/1.1 200 OK
<- access-control-allow-headers: Accept, Authorization, Content-Type, If-None-Match
<- access-control-allow-methods: HEAD, GET, POST, PUT, DELETE
<- cache-control: no-cache
<- content-encoding: gzip
<- content-type: application/json; charset=utf-8
<- date: Mon, 09 Mar 2020 14:58:31 GMT
<- set-cookie: session=10kq9qv1udf0107F4C70RY14fsum41sq50; HttpOnly; SameSite=Strict; Path=/
<- vary: origin,accept-encoding
<- x-microserv: NS4yNi4xODQuMjE3
<- x-poweredby: Poetry
<- Content-Length: 2346
<- Connection: keep-alive
该站点的文档说,如果成功,则会返回一个 JSON res,并在 res.data._id 中包含一个字符串标记。
我没有找到它...甚至查看res 的每个列表(和子列表)。
我应该如何找到令牌?
按照文档和 AngularJS 中的示例,我应该这样做:
// Create JSON Object with your token
let authorizeObject = {
'Authorization': 'Session ' + token,
'content-type': 'application/json;charset=UTF-8',
'accept': 'application/json,text/plain',
};
// Create header from the previous JSON Object
let header = {'headers':authorizeObject};
// Use the header in your http request...
$http.get('https://dashboard.server.eu/', header)
有什么让这个梦想成真的暗示吗?
更新 -- 使用 cURL,我可以检查是否返回了 _id 键/值……
使用命令:
curl -k -X POST "https://dashboard.server.eu/login" \
-d '{ "email" : "my@email.com", "password" : "mypass" }' \
-H "Content-Type: application/json"
我得到了输出:
{
"_id": "697v2on4ll0107F4C70RYhosfgtmhfug",
"isAuthenticated": true,
"user": {
"_id": "5dd57868d83cfc000ebbb273",
"firstName": "me",
"lastName": "Me",
...
所以,会话令牌确实在某个地方......
这对我有帮助吗?
【问题讨论】:
标签: r authentication session token httr