dpgbh20688 2016-10-17 12:11
浏览 85

Twitter发布请求时出现401错误

I can't get post requests to the twitter api to work. Login & GET requests seem to work fine though. I always get this reponse:

401 Authorization Required response:
{"errors":[{"code":32,"message":"Could not authenticate you."}]}

I'm using Laravel 5.3, Guzzle 6.3 and Guzzle/oauth-subscriber 0.3. Here's my code:

$stack = HandlerStack::create();

$middleware = new Oauth1([
    'consumer_key'    => env('TWITTER_CLIENT_ID'),
    'consumer_secret' => env('TWITTER_CLIENT_SECRET'),
    'token'           => env('TWITTER_TOKEN'),
    'token_secret'    => env('TWITTER_TOKEN_SECRET')
]);

$stack->push($middleware);

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

$res = $client->post(
  'account/update_profile_image.json?image=' . $avatar,
  [
    'auth' => 'oauth',
  ]
);

My app settings in Twitters' dev page is set to read & write, so that isn't the problem. Regenerated the keys once, no dice. What am I doing wrong here?

  • 写回答

1条回答 默认 最新

  • doubei2231 2016-10-20 14:59
    关注

    I fixed it, turns out you have to write the post request like this:

    $client->request(
          'POST', 
          'account/update_profile_image.json',
          [ 
          'multipart' => 
          [
            [
                'name'     => $name,
                'contents' => fopen($path), 'r'),
                'filename' => $fileName
            ],
          ], 
          'auth' => 'oauth', 
          //'debug' => true, 
          ]
        );
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路