简体   繁体   English

使用rCURL的API身份验证

[英]API authentication using rCURL

I am super new to R, I tried to connect to Rosette API via R and I got curl script below. 我对R超级陌生,我尝试通过R连接到Rosette API,并且在下面获得了curl脚本。 How do I internet this to R? 如何将其联网到R? I got my personal API key. 我有我的个人API密钥。

curl "https://api.rosette.com/rest/v1/ping" -H 'X-RosetteAPI-Key: [your_api-key]'

Thanks Peddie 谢谢佩迪

The curlconverter package was tailor-made for this. curlconverter包装是curlconverter目的量身定制的。

You can take your cURL command line and either copy it to the clipboard or pass it in directly. 您可以使用cURL命令行并将其复制到剪贴板或直接传递给剪贴板。 If you copy it to the clipboard then you call straighten() with no parameters: 如果将其复制到剪贴板,则不带任何参数调用straighten()

library(curlconverter)

flat <- straighten()

otherwise, you can pass it in as a string: 否则,您可以将其作为字符串传递:

flat <- straighten("curl 'https://api.rosette.com/rest/v1/ping' -H 'X-RosetteAPI-Key: [your_api-key]'")

That makes a list of all the URL parts which you can then pass into make_req() : 这样就列出了所有URL部分,然后您可以将它们传递到make_req()

req <- make_req(flat)[[1]]

make_req() turns that list into a fully functional httr call. make_req()将该列表变成功能齐全的httr调用。 It's vectorized, which is why it returns a list of one or more functions vs just a function. 它是向量化的,这就是为什么它返回一个或多个函数而不是一个函数的列表的原因。

If you just pass in just one object, then it also copies the generated function source to the clipboard, which you can paste back into your IDE. 如果只传递一个对象,那么它还会将生成的函数源复制到剪贴板,您可以将其粘贴回IDE中。 This one generates: 此生成:

httr::VERB(verb = "GET", url = "https://api.rosette.com/rest/v1/ping", 
    httr::add_headers(`X-RosetteAPI-Key` = "[your_api-key]"))

NOTE that you could also see that source by just entering req (no parens) at the R console. 注意,您也可以通过在R控制台中输入req (不带括号)来查看该源。

I usually examine the output and make it a bit more compact: 我通常检查输出并使其更紧凑:

GET(url = "https://api.rosette.com/rest/v1/ping", 
    add_headers(`X-RosetteAPI-Key` = "[your_api-key]"))

It imports/exports %>% so it's possible to — after copying a cURL command-line to the clipboard — do: 它导入/导出%>%因此可以-将cURL命令行复制到剪贴板后-执行:

straighten() %>% make_req() -> req

A fairy common subset of cURL command line options are supported. 支持cURL命令行选项的常用子集。 If one is missing that you need, just file an issue with an example. 如果缺少您所需要的,只需提出一个带有示例的问题 Speaking of examples, there are many more usage examples at the gh repo. 说到示例,gh repo中还有更多使用示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM