卷曲到 httr for Rosette API 无效方法

curl to httr for Rosette API invalid method

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

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 - 方法不允许 – 您试图使用无效方法访问 Rosette API。我不知道如何使用 httr 翻译上面的 curl 命令,有人可以一步一步地告诉我吗?

希望有人能帮忙 佩迪

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::GET.
  2. add_headers 命令的一部分
  3. curl中,-d命令引用了数据,而man page建议它发送一个POST请求以该数据作为内容。鉴于此,我们将 httr::POSTcontent 参数一起使用。

您可以替换此行:

`X-RosetteAPI-Key` = "",

... 使用您的适当密钥。由于我没有密钥,所以当我查看响应时我得到了未授权:

content(response)

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

嗯,虽然我说他们可以使用 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 上发布(当我有时间稍微完善它时)。