【问题标题】:Twitter API - Post status / tweet on behalf of userTwitter API - 代表用户发布状态/推文
【发布时间】:2018-06-01 10:19:13
【问题描述】:

在我的应用程序中,用户使用 Twitter OAuth 登录,这很好。我存储他们的访问令牌和秘密。我正在使用 Laravel。

我想在某些时候代表他们发布推文。在文档中,我需要的端点是 - 'statuses/update'。

对于身份验证,文档只是说 - “需要身份验证?是(仅限用户上下文)”

我似乎找不到太多关于“用户上下文”身份验证的确切含义以及我需要随请求发送的内容。我是 OAuth 的真正新手。

对此的任何帮助将不胜感激!

到目前为止,我得到了:

    $client = new GuzzleHttp\Client([
        'base_uri' => 'https://api.twitter.com/1.1/'
    ]);


    $response = $client->request('POST', 'statuses/update.json', [
        'status' => 'Whats up',
            'headers' => [
            'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
            'Authorization' => 'Bearer '.$user->token,
        ]
    ]);

返回:

“客户端错误:POST https://api.twitter.com/1.1/statuses/update.json 导致 401 Authorization Required 响应:↵{"errors":[{"code":89,"message":"无效或过期令牌。"}]}↵"

干杯!

【问题讨论】:

  • 你能告诉我们一些你写的代码吗?并且,如果可能的话,您收到的任何错误消息。
  • 您正在尝试在此处使用 Bearer 令牌,这是 Twitter 视为“仅限应用程序”的身份验证 - 您无法使用此设置代表用户发帖。您需要在 OAuth 标头中显示所有四个令牌(消费者密钥和机密、帐户令牌和帐户令牌机密)。见developer.twitter.com/en/docs/basics/authentication/guides/…

标签: laravel twitter


【解决方案1】:

抱歉,Bearer 可能只是应用程序身份验证

Bearer tokens allow for a more secure point of entry for developers to use the Twitter APIs. They are one of the core features of OAuth 2.0. Bearer tokens are a type of access token; authentication which uses bearer tokens is also known sometimes as application-only authentication or auth-only authentication.

https://developer.twitter.com/en/docs/basics/authentication/guides/bearer-tokens

您应该将 oauth 1.0 用于用户上下文 这意味着使用 Oauth .... 以用户身份发推文 您可以使用库支持生成authorization 将其添加到标题 或尝试手工制作请关注此https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2015-05-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多