使用R和httr包对Tradier进行API调用的问题

时间:2018-09-06 16:12:00

标签: r api httr tradier

我正在尝试对传统API进行API调用,但出现401错误。以下是其API的链接:https://developer.tradier.com/documentation/markets/get-quotes

这是我的简单代码:

library(httr)
tradier <- "https://api.tradier.com/v1/markets/history?symbol=AAPL"
getdata<-GET(url=tradier, add_headers(Authorization="Bearer XXXXXXXXXXXXXXXXX"))
getdata

1 个答案:

答案 0 :(得分:1)

sandbox API端点对我有用。我猜想对于brokerage API端点,您必须升级您的帐户。

library(magrittr)
library(httr)

token <- keyring::key_get("tradier_token")
url <- "https://sandbox.tradier.com/v1/markets/quotes?symbols=AAPL,MSFT"
resp <- GET(url = url, add_headers(Authorization = paste("Bearer", token)))
content(resp)[[1]] %>%
  data.table::rbindlist()
#>               V1             V2
#>  1:         AAPL           MSFT
#>  2:    Apple Inc Microsoft Corp
#>  3:            Q              Q
#>  4:        stock          stock
#>  5:       217.66         114.26
#>  6:        -2.38            0.7
#>  7:        -1.08           0.61
#>  8:     96246748       71229698
#>  9:     26803955       24017088
#> 10:     33973603       21492554
#> 11:  1.53756e+12    1.53756e+12
#> 12:       220.78            114
#> 13:       221.36         115.29
#> 14:       217.29         113.51
#> 15:       217.66         114.26
#> 16:       220.03         113.57
#> 17:       229.67          113.8
#> 18:       149.16          72.92
#> 19:       217.75         113.81
#> 20:            1              9
#> 21:            K              P
#> 22: 1.537574e+12   1.537574e+12
#> 23:       218.02         113.95
#> 24:            2              3
#> 25:            P              P
#> 26: 1.537574e+12   1.537574e+12
#> 27:         AAPL           MSFT
#>               V1             V2

reprex package(v0.2.1)于2018-09-23创建