2016-10-10 58 views
0

所以,我終於得到我的ping命令使用HTTR如下面的腳本捲曲HTTR的玫瑰花API無效的方法

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

偉大的東西的工作,但現在COS林在下一位stucked。

現在我想調用另一個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 - 不允許的方法 - 你試圖用一個無效的方法來訪問玫瑰花API。我不知道如何使用httr來翻譯上面的curl命令有人可以通過一步一步來談談我嗎?

希望有人能幫助取悅 佩迪

+0

您知道Rosette爲其API提供了[R package](https://github.com/rosette-api/R-Binding),對不對? – hrbrmstr

回答

1

好,而我的意見,他們具有R代碼中使用是正確的,這是horribad 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:玫瑰花API categorizatioin服務
  • ros_embedding:玫瑰花API文本嵌入服務
  • ros_entities:Rosette API實體提取服務
  • ros_info:玫瑰花API版本信息
  • ros_language:玫瑰花API語言識別服務
  • ros_make_name:做一個名字對象
  • ros_morph:玫瑰花API形態分析服務
  • ros_name_similarity:玫瑰花API版本信息
  • ros_name_translation :Rosette API名稱翻譯服務
  • ros_ping Rosette:API可用性
  • ros_relationships:玫瑰花API關係提取服務
  • ros_sentences:玫瑰花API句子判定服務
  • ros_sentiment:玫瑰花API情感分析服務
  • ros_tokens:玫瑰花API tokenizatioin服務

將在CRAN一個這個月晚些時候(當我有時間研磨一下時)。

+0

我不熟悉腳本的devtools :: instal_github(「hrbrmstr/rosette」)部分,所以我只是將它複製並粘貼到R控制檯中? – PeddiePooh

+0

。 (我也修正了錯字)。我正在與玫瑰花人合作,很快就能在CRAN上獲得它 – hrbrmstr

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開頭。這些是標題語句,因此成爲httr::GET中的add_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" 
+0

這真的很有用一步一步謝謝。當我把我的API我有一個消息錯誤在curl :: curl_fetch_memory(URL,句柄=句柄): 無法連接到服務器....? – PeddiePooh