在R中使用授权标头的curl请求

时间:2018-01-04 18:32:15

标签: r curl rcurl

curl --include \
     --header "Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" \
     "https://onesignal.com/api/v1/notifications?app_id={appId}"

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

1 个答案:

答案 0 :(得分:1)

为此我使用了 curl 包而不是 RCURL。

我的目标是使用来自 OANDA 的标头 CURL 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"
}