6

I'm using meltingice's API for TwitPic and when I try to upload a picture I get a 401 error with the message "Could not authenticate you (header rejected by twitter)".

My headers (retrieved from the HTTP Request2 object) are:

Array
(
    [user-agent] => HTTP_Request2/2.0.0 (http://pear.php.net/package/http_request2) PHP/5.2.17
    [x-verify-credentials-authorization] => OAuth realm="http://api.twitter.com/", oauth_consumer_key="****************", oauth_signature_method="HMAC-SHA1", oauth_token="#########-******************", oauth_timestamp="1325192643", oauth_nonce="***********", oauth_version="1.0", oauth_signature="****************%3D"
    [x-auth-service-provider] => https://api.twitter.com/1/account/verify_credentials.json
    [content-type] => multipart/form-data
)

I made sure that the verify_credentials signature is using GET, and I can't see any other issues.

What am I doing wrong?

Thanks :)

EDIT: Here's my source code.

$venue = $this->Venue->findById($venueId);
$twitterData = json_decode($venue['Venue']['twitter_data']);
$token = $twitterData->token;
$secret = $twitterData->secret;
$this->Twitter->loginTwitterUser($token, $secret);
require_once(WWW_ROOT.'twitpic/TwitPic.php');

$twitpic = new TwitPic('**********', '*******', '*********', $token, $secret);


$result['result'] = $twitpic->upload(array('media'=> '/home/todays/public_html/tsm/app/webroot/files/uploads/LOGOA7V1_10.png', 'message'=> 'test'));

And I'm sure that the token, secret, and app credentials are correct as they work in my Twitter API without any problems. I've also double checked the Twitpic API key.

Nick Badal
  • 681
  • 1
  • 8
  • 26
  • Try surrounding your code with try and catch to see if it throws an exception. Also you should enable errors to see if there's any errors that you do not see. And also: Why are you logging into twitter aswell? Is that really necessary when you use TwitPic? – Runar Jørgensen Jan 13 '12 at 16:31
  • Try getting the actual HTTP request being sent to TwitPic. There are several ways you could do this: netcat, fiddler, wireshark... – Samuel Edwin Ward Jan 13 '12 at 16:48
  • @RunarJørgensen It throws a custom exception, and I went into where the exception was thrown to receive the header array. – Nick Badal Jan 16 '12 at 05:37
  • @SamuelEdwinWard Those headers are the only thing in the request, as I've looked in the plugin's code to retrieve them. I wouldn't be able to use any of those tools as this is hosted on a basic web host. – Nick Badal Jan 16 '12 at 05:37
  • @nickbadal You could run a netcat listener on your workstation and temporarily change the server in your code to point there instead of twitpic. – Samuel Edwin Ward Jan 17 '12 at 15:02

1 Answers1

2

After checking the TwitPic documentation I noticed that the 401 error was explained: This method will return a 401 Unauthorized if the OAuth header was not present or could not be verified with Twitter.

You're saying you made sure that the verify_credentials signature is using GET while the API only accepts POST. Maybe that's your problem?

Here's the example code related to the API you're using:

Runar Jørgensen
  • 564
  • 3
  • 13