本文介绍了API 1.1 使用 r 请求 Twitter 不记名令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个论坛并尝试了一些似乎相关的东西,但没有成功.如果有人能发现我遗漏的东西,我将不胜感激.

I have searched this forum and tried several things that seemed relevant, but with no success. If anyone can spot what I'm missing I would be very grateful.

我正在尝试使用仅应用程序授权获取不记名令牌,如 https 所述://dev.twitter.com/docs/auth/application-only-auth 以便我可以获取关注者的 s/id.

I am trying to get a bearer token using application only authorization as explained at https://dev.twitter.com/docs/auth/application-only-auth so that I can GET follower s/ids.

我使用 rstudio 和我的消费者密钥在 r 中构建了一个请求Base64 编码形式的秘密.

I have constructed a request in r using rstudio with my consumer key & secret in Base64 encoded form.

library(httr)
POST(url="https://api.twitter.com/oauth2/token", config=add_headers(
c('Host="api.twitter.com"',
'User-Agent="NameOfMyApp"',
'Authorization="Basic MyKeyandSecretBase64Encoded"',
'Content-Type="application/x-www-form-urlencoded;charset=UTF-8"',
'Content-Length="29"',
'Accept-Encoding="gzip"')), body="grant_type=client_credentials")

我收到的回复是:

Response [https://api.twitter.com/oauth2/token]
Status: 403
Content-type: application/json; charset=utf-8
{"errors":[{"label":"authenticity_token_error","code":99,"message":"Unable to verify your credentials"}]}

我尝试重置我的凭据,但没有任何区别.

I tried resetting my credentials but it made no difference.

推荐答案

我迟到了几个星期,但对于像我这样偶然发现这个页面的人来说,这里有一些代码对我有用,返回一个不记名令牌:

I'm a few weeks late, but for anyone like me who stumbles across this page, here is some code that works for me, returning a bearer token:

POST(url="https://api.twitter.com/oauth2/token",
config=add_headers(c("Host: api.twitter.com",
"User-Agent: [app name]",
"Authorization: Basic [base64encoded]",
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: 29",
"Accept-Encoding: gzip")),
body="grant_type=client_credentials")

一旦你有了一个不记名令牌,你就可以像这样把它放在 GET 的头部:

Once you have a bearer token, you put it in the header of a GET like so:

GET("https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=justinbieber&count=5000",
config=add_headers(c("Host: api.twitter.com",
"User-Agent: [app name]",
"Authorization: Bearer [bearer token]",
"Accept-Encoding: gzip")))

这篇关于API 1.1 使用 r 请求 Twitter 不记名令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 07:28