curl to httr for Rosette API无效方法

时间:2016-10-10 20:06:10

标签: r curl httr

所以我终于让我的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 - 方法不允许 - 您尝试使用无效方法访问Rosette API。我不知道如何使用httr翻译上面的curl命令有人可以一步一步地跟我说话吗?

希望有人可以帮忙 佩迪

2 个答案:

答案 0 :(得分:2)

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 语句,因此成为add_headershttr::GET命令的一部分。
  2. curl中,-d命令引用数据,而man page表示它发送POST请求,并将该数据作为内容。鉴于此,我们将httr::POSTcontent参数一起使用。
  3. 您可以替换此行:

    `X-RosetteAPI-Key` = "",
    

    ...用你合适的钥匙。由于我没有密钥,当我看到响应时,我未经授权:

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

答案 1 :(得分:1)

嗯,虽然我的评论是他们有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:创建名称对象
  • 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上(当我有时间对它进行一点修改时)。