跟随某人在R中使用Twitter Rest Api

时间:2016-06-01 05:31:28

标签: r rest twitter

我正在通过Twitter发送this文档来关注某人。我已使用twitteRapi_keyaccess_token等授权该帐户。由于这是POST操作,我决定在R中使用httr包。文档中提供的示例之一是

  

https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true

因此,只需将user_id更改为我想要关注的帐户。

library(httr)
POST("https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true",verbose())

其中1401881是我想要关注的ID。

这给了我

-> POST /1.1/friendships/create.json?user_id=1401881&follow=true HTTP/1.1
-> User-Agent: libcurl/7.39.0 r-curl/0.9.1 httr/1.1.0
-> Host: api.twitter.com
-> Accept-Encoding: gzip, deflate
-> Cookie: guest_id=v1%3A146475568975546263
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 0
-> 
<- HTTP/1.1 400 Bad Request
<- content-encoding: gzip
<- content-length: 87
<- content-type: application/json; charset=utf-8
<- date: Wed, 01 Jun 2016 05:15:42 GMT
<- server: tsa_b
<- strict-transport-security: max-age=631138519
<- x-connection-hash: 6abd7db7f4c47058bf9d96e9ae23fb83
<- x-response-time: 5
<- 
Response [https://api.twitter.com/1.1/friendships/create.json? user_id=1401881&follow=true]
Date: 2016-06-01 05:15
Status: 400
Content-Type: application/json; charset=utf-8
Size: 62 B

从响应消息中可以看出它显示Bad Request我认为我生成的URL是错误的。我也试过

POST("https://api.twitter.com/1.1/friendships/create", verbose(), 
    body = list(user_id = "101311381"), encode = "json")

我尝试了其他各种方法,并尝试使用谷歌搜索,但无法找到解决方案。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试将oauth_token(从twitteR包生成)添加到POST请求

library(httr)
POST("https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true", 
           config(token = oauth_token))