【问题标题】:API 1.1 requesting twitter bearer token using rAPI 1.1 使用 r 请求 twitter 不记名令牌
【发布时间】:2013-12-19 18:46:14
【问题描述】:

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

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

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

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"}]}

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

【问题讨论】:

  • 您能否告诉我您的请求中的密码和密码在哪里,以便从身份验证服务器获取不记名令牌?谢谢,米格

标签: r twitter oauth twitter-oauth


【解决方案1】:

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

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 的头部,如下所示:

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】:

回复晚了,但现有的答案对我不起作用。所以这里有一个修改GET请求的解决方案。

add_headers() 使用命名向量。这需要用反引号 (``) 括起带连字符的标题名称。所以你的POST() 电话应该是:

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

在成功响应后,可以通过以下方式访问应用程序访问令牌:

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

然后您可以通过GET 请求验证这一点,例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2014-05-04
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 2021-08-02
    • 2017-03-20
    相关资源
    最近更新 更多