【问题标题】:curl to httr for Rosette API invalid method对于 Rosette API 无效方法,curl 到 httr
【发布时间】:2016-10-12 05:19:01
【问题描述】:

所以我终于让我的 ping 命令使用 httr 作为下面的脚本工作

library(httr)
curl_com  = GET("https://api.rosette.com/rest/v1/ping", add_headers(`X-RosetteAPI-Key` = "my api"))

很棒的东西,但现在因为我卡在了下一点。

现在我想调用另一个api来做情绪分析

curl -X POST \
-H "X-RosetteAPI-Key: your_api_key" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Cache-Control: no-cache" \
-d '{"content": "Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”" }' \
"https://api.rosette.com/rest/v1/sentiment"

我收到错误 405 - Method Not Allowed - 您尝试使用无效方法访问 Rosette API。我不知道如何使用 httr 翻译上面的 curl 命令,有人可以一步一步告诉我吗?

希望有人可以帮忙 佩迪

【问题讨论】:

  • 您知道 Rosette 的 API 有一个 R package,对吧?

标签: r curl httr


【解决方案1】:

curl 命令到httr 的简单转换具有如下所示的最终结果:

response = POST("https://api.rosette.com/rest/v1/sentiment",
                add_headers(
                  `X-RosetteAPI-Key` = "",
                  `Content-Type` = "application/json",
                  Accept = "application/json",
                  `Cache-Control` = "no-cache"
                ),
                content = list(content = "Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, 'The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.'"))

让我们来看看吧。

  1. 在您原来的curl 命令中,有四行以-H 开头。这些是header 语句,因此成为httr::GETadd_headers 命令的一部分。
  2. curl 中,-d 命令指的是数据man page 建议它发送一个POST 请求,该数据作为内容。鉴于此,我们使用 httr::POSTcontent 参数。

你可以替换这一行:

`X-RosetteAPI-Key` = "",

... 使用适当的密钥。由于我没有钥匙,所以当我查看回复时,我得到了未经授权:

content(response)

#> $code
#> [1] "unauthorized"
#> 
#> $message
#> [1] "authentication required to access this resource"

【讨论】:

  • 这一步一步非常有用,谢谢。当我将 API 放入时,我收到一条消息 Error in curl::curl_fetch_memory(url, handle = handle) : Couldn't connect to server ....?
【解决方案2】:

好吧,虽然我对他们有 R 代码可以使用的评论是正确的,但这是可怕的 R 代码。

您现在只需使用 rosette R 包即可访问完整的 API。只需确保将您的 Rosette API 密钥放入 ROSETTE_API_KEY(最简单的方法就是编辑 ~/.Renviron)。

devtools::install_github("hrbrmstr/rosette")

ros_sentiment("Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, 'The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.'")
## $document
## $document$label
## [1] "pos"
## 
## $document$confidence
## [1] 0.7962072
## 
## 
## $entities
##           type            mention         normalized count entityId sentiment.label sentiment.confidence
## 1       PERSON        Dan Aykroyd        Dan Aykroyd     2  Q105221             pos            0.6385089
## 2 ORGANIZATION Hollywood Reporter Hollywood Reporter     1   Q61503             pos            0.5338094

API 的其余部分也在那里:

  • rosette_api_key: 获取或设置 ROSETTE_API_KEY 值
  • ros_categories: Rosette API 分类服务
  • ros_embedding: Rosette API 文本嵌入服务
  • ros_entities: Rosette API 实体提取服务
  • ros_info: Rosette API 版本信息
  • ros_language: Rosette API 语言识别服务
  • ros_make_name:创建一个 Name 对象
  • ros_morph: Rosette API 形态分析服务
  • ros_name_similarity: Rosette API 版本信息
  • ros_name_translation: Rosette API 名称翻译服务
  • ros_ping Rosette:API 可用性
  • ros_relationships: Rosette API 关系提取服务
  • ros_sentences: Rosette API 句子判断服务
  • ros_sentiment: Rosette API 情绪分析服务
  • ros_tokens: Rosette API 代币化服务

它将在本月晚些时候在 CRAN 上发布(当我有时间稍微完善它时)。

【讨论】:

  • 我不熟悉脚本的 devtools::instal_github("hrbrmstr/rosette") 部分,所以我只是将其复制并粘贴到 R 控制台中吗?
  • 是的。 (我还修正了错字)。我正在与 Rosette 的人合作,尽快将其用于 CRAN
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 1970-01-01
相关资源
最近更新 更多