dqc42632 2010-04-29 21:36
浏览 40
已采纳

使用OAuth更新twitter个人资料图片

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);

  $ch = 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.

  • 写回答

1条回答 默认 最新

  • duanlou7910 2010-04-30 21:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来