douhuan1979 2012-09-25 05:23 采纳率: 100%
浏览 57

在Twitter中获取永久用户访问令牌

I'm using this php library for interacting with twitter api called tmhOAuth https://github.com/themattharris/tmhOAuth and I'm having trouble getting a permanent user access token and token secret. I think the one's that I'm currently getting lasts only for the current browser tab. In the code below I'm assigning the user token and user secret to a session.

function access_token($tmhOAuth) {
  $tmhOAuth->config['user_token']  = $_SESSION['oauth']['oauth_token'];
  $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];

  //store user token and secret to a session to be accessed on a different page
  $_SESSION['u_token'] =  $tmhOAuth->config['user_token'];
  $_SESSION['u_secret'] = $tmhOAuth->config['user_secret'];  

  $code = $tmhOAuth->request(
    'POST',
    $tmhOAuth->url('oauth/access_token', ''),
    array(
      'oauth_verifier' => $_REQUEST['oauth_verifier']
    )
  );

  if ($code == 200) {
    $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
    unset($_SESSION['oauth']);
    header('Location: ' . tmhUtilities::php_self());
  } else {
    outputError($tmhOAuth);
  }
}

Then I just access the user token and secret from another page:

$tmhOAuth = new tmhOAuth(array(
  'consumer_key'    => 'xxx',
  'consumer_secret' => 'xxx',
  $_SESSION['u_token'],
 $_SESSION['u_secret']
));

$tweet = 'tweet';

$code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
  'status' => $tweet
));

echo json_encode($code); //I always get 401 here
?>

The problem is I always get the 401 response.

  • 写回答

1条回答 默认 最新

  • drgdn82648 2012-10-09 22:27
    关注

    Did you try to add

    'oauth_token' => $_REQUEST['oauth_token']
    

    to your request?

    $code = $tmhOAuth->request(
        'POST',
        $tmhOAuth->url('oauth/access_token', ''),
        array(
          'oauth_verifier' => $_REQUEST['oauth_verifier'],
          'oauth_token' => $_REQUEST['oauth_token']
        )
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?