问题
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:
"error could not authenticate you" - probably means your OAuth signature base string is not correct
"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
- "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
- "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:
The request should have content-type of multipart/form-data
The request body should contain the two elements twitter expects media[] and status in the correct format (RFC 2388)
- In addition Twitter server expects \r\n (CR LF) after each portion of the request body. Skipping this causes Twitter to return an error
- The media[] data should be base64 encoded and the content-disposition section in the request body should have Content-Transfer-Encoding
- 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