Twitter发布请求时出现401错误

时间:2016-10-17 12:11:17

标签: php laravel twitter twitter-oauth guzzle

我无法向twitter API发送请求。登录& GET请求似乎工作正常。我总是得到这个回应:

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

我使用的是Laravel 5.3,Guzzle 6.3和Guzzle / oauth-subscriber 0.3。这是我的代码:

$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',
  ]
);

我在Twitters中的应用设置' dev页面设置为read&写,所以这不是问题。重生钥匙一次,没有骰子。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我修好了,原来你必须写这样的帖子请求:

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