开发者

Update twitter profile image using OAuth

I'm trying to get twitter update_profile_image to work using OAuth. I was using curl with regular authentication and everything was working fine, but I switched to OAuth using this library, and now everything except update_profile_image works.

I read something about twitter OAuth having problems with multipart data, but that was a while ago and the plugin is supposed to have dealt with that issue.

My working regular authentication with curl code

$url      = 'http://api.twitter.com/1/account/update_profile_image.xml';
  $uname    = $_POST['username'];
  $pword    = $_POST['password'];
  $img_path = 'xxx';

  $userpwd  = $uname . ':' . $pword;
  $img_post = array('image' => '@' . $img_path . ';type=image/jpeg',
            'tile'  => 'true');
  $format = 'xml'; //alternative: json
  $message = 'Test update with a random num'.rand();

  $opts = array(CURLOPT_URL => $url,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_HEADER => true,
          CURLOPT_POST => true,
          CURLOPT_POSTFIELDS => $img_post,
          CURLOPT_HTTPAUTH => CURLAUTH_ANY,
          CURLOPT_USERPWD => $userpwd,
          CURLOPT_HTTPHEADER => array('Expect:'),
          CURLINFO_HEADER_OUT => true);

  $c开发者_运维问答h = curl_init();
  curl_setopt_array($ch, $opts);
  $response = curl_exec($ch);
  $err      = curl_error($ch);
  $info     = curl_getinfo($ch);
  curl_close($ch);

My current OAuth code [I had to cut it down, so do not minor look for syntax errors]

include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);

try{
  $img_path = 'xxx';
  //$twitterObj->post_accountUpdate_profile_image(array('@image' => "@".$img_path));    

   $twitterObj->post('/account/update_profile_image.json', array('@image' => "@".$img_path));

   $twitterObj->post_statusesUpdate(array('status' => 'This is my new status:'.rand())); //This works
   $twitterInfo= $twitterObj->get_accountVerify_credentials();  
   echo $twitterInfo->responseText;
}catch(Exception $e){
    echo $e->getMessage();
  }

I've been trying to figure this out for a while, ANY help would be greatly appreciated. I'm not in any way tied to this library, so feel free to recommend others.


The version of the library I was using was outdated. Once I updated, I had to deal with a couple of other issues including 401 error due to a wrong server time, and now everything works fine. Printing out the $response->responseText helps a lot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜