【问题标题】:How to get session token when authenticating to JSON REST API (in R)对 JSON REST API 进行身份验证时如何获取会话令牌(在 R 中)
【发布时间】: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


    【解决方案1】:

    在您的问题中查看res 的图像,消息 那里,在content 下 - 只是内容存储为原始字节的向量,这就是为什么您没有将其识别为json。

    由于任何文件类型都可以通过 http 发送,httr 响应对象中的内容由于各种原因以原始格式而不是字符串存储 - 也许最重要的是因为许多二进制文件将包含 0x00 字节, 在 R 中的字符串中是不允许的。

    在您的情况下,我们不仅可以判断 res$content 文本,而且它是您的“缺失”json。 res$content 的前六个字节显示在您的图像中,并且是 7b, 22, 5f, 69, 64, 22。我们可以通过以下方式将它们转换为 R 中的字符串:

    rawToChar(as.raw(c(0x7b, 0x22, 0x5f, 0x69, 0x64, 0x22)))
    [1] "{\"_id\""
    

    这匹配您预期的 json 字符串的前六个字符。

    因此,如果你这样做:

    httr::content(res, "text")
    

    rawToChar(res$content)
    

    你会得到你的 json 作为一个字符串。

    【讨论】:

    • 亲爱的@Allan,明天再来找你。需要检查。谢谢!
    • 正是我需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2014-06-08
    • 2016-12-02
    • 2019-01-18
    • 2014-04-14
    • 1970-01-01
    • 2019-08-19
    相关资源
    最近更新 更多