使用密钥连接到R中的Rest API [英] Connect to Rest API in R with key

查看:53
本文介绍了使用密钥连接到R中的Rest API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题,但我仍然不知道.我想用我的API密钥连接到REST API.我浏览了 httr jsonlite 和其他文档,但仍然不知道如何设置API密钥.

this is a simple question, but one that I still can't figure out. I want to connect to a REST API with my API key. I've looked through the documentation on httr, jsonlite and others and still can't figure out how to set the API key.

这是端点-https://api.tiingo.com/tiingo/daily//prices?startDate=2012-1-1&endDate=2016-1-1?

This is the endpoint - https://api.tiingo.com/tiingo/daily//prices?startDate=2012-1-1&endDate=2016-1-1?

我尝试在此URL上使用 GET 函数,并在调用中将我的API密钥指定为 key .我也尝试过 api_key = key .我总是收到401错误.

I've tried using the GET function on this URL and specify my API key as key in the call. I've also tried api_key = key. I always get a 401 error back.

谢谢

推荐答案

API需要一个 Authorization 标头,其中包含 Token yOuRAsSiGnEdT0k3n .您应该将令牌存储在类似环境变量的位置,这样它就不会卡在脚本中.我使用 TIINGO_TOKEN 并将其放入〜/.Renviron .

The API is expecting a Authorization header with Token yOuRAsSiGnEdT0k3n in it. You should store the token in something like an environment variable so it's not stuck in scripts. I used TIINGO_TOKEN and put it into ~/.Renviron.

您可以创建一个辅助函数,以减少调用的麻烦:

You can make a helper function to make the calls less mudane:

library(httr)
library(jsonlite)
library(tidyverse)
library(hrbrthemes)

get_prices <- function(ticker, start_date, end_date, token=Sys.getenv("TIINGO_TOKEN")) {

  GET(
    url = sprintf("https://api.tiingo.com/tiingo/daily/%s/prices", ticker),
    query = list(
      startDate = start_date,
      endDate = end_date
    ),
    content_type_json(),
    add_headers(`Authorization` = sprintf("Token %s", token))
  ) -> res

  stop_for_status(res)

  content(res, as="text", encoding="UTF-8") %>%
    fromJSON(flatten=TRUE) %>%
    as_tibble() %>%
    readr::type_convert()

}

现在,您只需传递参数即可:

Now, you can just pass in parameters:

xdf <- get_prices("googl", "2012-1-1", "2016-1-1")

glimpse(xdf)
## Observations: 1,006
## Variables: 13
## $ date        <dttm> 2012-01-03, 2012-01-04, 2012-01-05, 2012-01-06, 2...
## $ close       <dbl> 665.41, 668.28, 659.01, 650.02, 622.46, 623.14, 62...
## $ high        <dbl> 668.15, 670.25, 663.97, 660.00, 647.00, 633.80, 62...
## $ low         <dbl> 652.3700, 660.6200, 656.2300, 649.7900, 621.2300, ...
## $ open        <dbl> 652.94, 665.03, 662.13, 659.15, 646.50, 629.75, 62...
## $ volume      <int> 7345600, 5722200, 6559200, 5380400, 11633500, 8782...
## $ adjClose    <dbl> 333.7352, 335.1747, 330.5253, 326.0164, 312.1937, ...
## $ adjHigh     <dbl> 335.1095, 336.1627, 333.0130, 331.0218, 324.5017, ...
## $ adjLow      <dbl> 327.1950, 331.3328, 329.1310, 325.9010, 311.5768, ...
## $ adjOpen     <dbl> 327.4809, 333.5446, 332.0901, 330.5955, 324.2509, ...
## $ adjVolume   <int> 3676476, 2863963, 3282882, 2692892, 5822572, 43955...
## $ divCash     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ splitFactor <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...

而且,它有效":

ggplot(xdf, aes(date, close)) +
  geom_segment(aes(xend=date, yend=0), size=0.25) +
  scale_y_comma() +
  theme_ipsum_rc(grid="Y")

对于其他API端点,您可以遵循此惯用法.完成后,请考虑制作一个包装,以便社区可以使用您所获得的知识.

You can follow this idiom for the other API endpoints. When done, consider making a package out of it so the community can use what the knowledge you gained.

您可以执行一些额外的步骤,并实际制作带有日期或数字参数的函数,以实际采用这些R对象类型,并在输入时对其进行验证.

You can go some extra steps and actually make functions that take date or numeric parameters to actually take those type of R objects and validate them on input, too.

这篇关于使用密钥连接到R中的Rest API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆