【问题标题】:Follow someone using twitter Rest Api in R在 R 中使用 twitter Rest Api 关注某人
【发布时间】:2016-06-01 05:31:28
【问题描述】:

我正在浏览 Twitter 上的 this 文档以关注某人。我已经使用带有api_keyaccess_token 等的twitteR 包授权帐户。由于这是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")

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

【问题讨论】:

  • 我认为只有当该帐户还具有 API 的授权访问权限时,您才能关注某人。这适用于所有事情,例如张贴在任何句柄和其他类型的东西上。
  • @ChirayuChamoli 我无法在 R 中做到这一点。不过在 Python 中非常简单。

标签: r rest twitter


【解决方案1】:

尝试将您的 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))

【讨论】:

  • oauth_token 是什么?是setup_twitter_oauth函数产生的输出吗?
猜你喜欢
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
相关资源
最近更新 更多