Twitter api 1.1 update_with_media

ε祈祈猫儿з 提交于 2019-12-31 02:21:11

问题


i'm changing my php code to be compatible with new API and i'm stuck with update_with_media. This is my code:

$image = constant('PATH_UPLOAD').$db_data['post_image'];
$connection = new TwitterOAuth(constant('CONSUMER_KEY'), constant('CONSUMER_SECRET'), $db_data['tw_oauth_token'], $db_data['tw_oauth_secret']);          
$content = $connection->OAuthRequest('https://api.twitter.com/1.1/account/verify_credentials.json', 'GET', array());
$twitterInfo = json_decode($content);                      
$resp_tw = $connection->OAuthRequest('https://api.twitter.com/1.1/statuses/update_with_media.json', 'POST', 
             array(
               'status'   => html_entity_decode($db_data['post_text'],ENT_QUOTES,'UTF-8'),              
                 'media[]'  => "@{$image}"
             )         
           );                          

And it returns

{"errors":[{"code":189,"message":"Error creating status"}]}

What might be the problem / what i'm doing wrong?


回答1:


you can try like this :

$tmhOAuth = new tmhOAuth(array(
  'consumer_key' => 'abc',
  'consumer_secret' => 'abc',
  'user_token' => 'abc',
  'user_secret' => 'abc',
));

$response = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update_with_media'),
array(
       'status' => $message,
       'media[]'  => file_get_contents($image)
));
if ($response != 200) {
    //Do something if the request was unsuccessful
}

there is my code test https://twitter.com/wallapps/status/357137553691906048




回答2:


Though the question is a few months old, I thought I'd answer it since I spent several hours on making update_with_media work and could not find satisfactory answers online.

Twitter API error messages unfortunately are not that specific. I was able to figure out the foll twitter errors:

  1. "error could not authenticate you" - probably means your OAuth signature base string is not correct

  2. "error incorrect or missing uri" - probably means you are not strictly following the format expected by Twitter in the request body. Could be something as simple as missing a \n in your request body

  3. "error creating status - probably means your status text is encoded when it should not be in the request body of a multipart / form-data request
  4. "Error internal error" - this one indicates nothing useful. It probably means that you have some data that is not encoded when twitter expects it to be or vice versa. It could also mean that you have not included the encoding type in the request body

To make update_with_media work, these tips may help:

  1. The request should have content-type of multipart/form-data

  2. The request body should contain the two elements twitter expects media[] and status in the correct format (RFC 2388)

  3. In addition Twitter server expects \r\n (CR LF) after each portion of the request body. Skipping this causes Twitter to return an error
  4. The media[] data should be base64 encoded and the content-disposition section in the request body should have Content-Transfer-Encoding
  5. OAuth signature base string should be: (see OAuth 1.0A spec for signature) &All oauth_ parameters(name=value) in the Auth header of your request object in alphabetic order, separated by & and encoded


来源:https://stackoverflow.com/questions/17081992/twitter-api-1-1-update-with-media

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