API 1.1 requesting twitter bearer token using r

谁说我不能喝 提交于 2020-08-11 17:59:57

问题


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.

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.

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")

In response I receive:

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.


回答1:


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")

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")))



回答2:


A late response, but the existing answer wasn't working for me. So here's a solution with a modification of the GET request.

add_headers() uses a named vector. This requires the hyphenated header names to be bracketed with backticks (``). So your POST() call should be:

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

Within a successful response the application access token can be accessed with:

bearer_token <- jsonlite::fromJSON(rawToChar(response$content))$access_token

You can then verify this with a GET request, such as:

GET("https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=justinbieber&count=100",
    config = add_headers(.headers = c(Host = "api.twitter.com",
                                      `User-Agent` = "NameOfMyApp",
                                      Authorization = paste("Bearer", bearer_token),
                                     `Accept-Encoding` = "gzip")))


来源:https://stackoverflow.com/questions/20355758/api-1-1-requesting-twitter-bearer-token-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!