【问题标题】:curl request with authorization header in R在 R 中带有授权标头的 curl 请求
【发布时间】:2018-01-04 18:32:15
【问题描述】:
curl --include \
     --header "Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" \
     "https://onesignal.com/api/v1/notifications?app_id={appId}"

我正在查看 RCurl 包,但无法理解在 api 调用中包含上述标头的方法。我想知道如何在 R 中的 api 调用中包含此标头。

【问题讨论】:

  • this 覆盖它吗?
  • 您到底尝试了什么?显示您的 R 代码,我们可以指出问题所在。
  • @Mako212:谢谢。这有帮助。如何从此响应中获取数据:响应 [onesignal.com/api/v1/… 日期:2018-01-04 19:33 状态:200 内容类型:应用程序/json; charset=utf-8 大小:86.6 kB

标签: r curl rcurl


【解决方案1】:

我已经使用 curl 包而不是 RCURL。

我的目标是使用来自 OANDA 的 headers 卷曲 GET 请求以获得价格响应。

我打算运行的 curl 命令:

curl \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ****api_key***" \
  "https://api-fxpractice.oanda.com/v3/accounts/{account_id}/instruments?instruments=EUR_USD"

R 等效项:

library(curl)
h <- new_handle(verbose = TRUE)
handle_setheaders(h,
 "Content-Type" = "application/json",
 "Authorization" = "Bearer ****api_key****"
)
con <- curl("https://api-fxpractice.oanda.com/v3/accounts/{account_id}/instruments?instruments=EUR_USD", handle = h)
jsonlite::prettify(readLines(con))

输出:

    "instruments": [
        {
            "name": "EUR_USD",
            "type": "CURRENCY",
            "displayName": "EUR/USD",
            "pipLocation": -4,
            "displayPrecision": 5,
            "tradeUnitsPrecision": 0,
            "minimumTradeSize": "1",
            "maximumTrailingStopDistance": "1.00000",
            "minimumTrailingStopDistance": "0.00050",
            "maximumPositionSize": "0",
            "maximumOrderUnits": "100000000",
            "marginRate": "0.02",
            "guaranteedStopLossOrderMode": "DISABLED",
            "tags": [
                {
                    "type": "ASSET_CLASS",
                    "name": "CURRENCY"
                }
            ],
            "financing": {
                "longRate": "-0.0178",
                "shortRate": "-0.0031",
                "financingDaysOfWeek": [
                    {
                        "dayOfWeek": "MONDAY",
                        "daysCharged": 1
                    },
                    {
                        "dayOfWeek": "TUESDAY",
                        "daysCharged": 1
                    },
                    {
                        "dayOfWeek": "WEDNESDAY",
                        "daysCharged": 1
                    },
                    {
                        "dayOfWeek": "THURSDAY",
                        "daysCharged": 1
                    },
                    {
                        "dayOfWeek": "FRIDAY",
                        "daysCharged": 1
                    },
                    {
                        "dayOfWeek": "SATURDAY",
                        "daysCharged": 0
                    },
                    {
                        "dayOfWeek": "SUNDAY",
                        "daysCharged": 0
                    }
                ]
            }
        }
    ],
    "lastTransactionID": "3"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多