0

我正在开发我的第一个 twitter 应用程序。我设法获得 request_token,并在 twitter 登录后成功重定向。但我的问题是我无法获得显示 401 响应的访问令牌。

session_start();
require_once('tmhOAuth.php');

$connection = new tmhOAuth(array(
'consumer_key' => '****',
'consumer_secret' => '****'

 ));

$connection->request('POST',$connection->url('oauth/request_token',""),array('oauth_callback' =>"http://www.123.in/twitter/twitterin.php"));

$result=$connection->extract_params($connection->response['response']);
$_SESSION['oauth_token']=$result['oauth_token'];
$_SESSION['oauth_token_secret']=$result['oauth_token_secret'];
$url='https://api.twitter.com/oauth/authenticate?oauth_token='.$result['oauth_token'];
header("Location:". $url);

上面的代码工作正常,但在重定向页面 twitter/twitterin.php

session_start();
require_once('tmhOAuth.php');

$connection = new tmhOAuth(array(
'consumer_key' => '****',
'consumer_secret' => '****'

 ));

$oauth_token=$_GET['oauth_token'];
$oauth_verifier=$_GET['oauth_verifier'];

 $connection->config["user_token"]=$_SESSION['oauth_token'];
 $connection->config["oauth_token_secret"]=$_SESSION['oauth_token_secret'];

$connection->request("POST", $connection-      >url("oauth/access_token",""),array("oauth_verifier"=>$oauth_verifier));
var_dump($connection->response);

我收到 401 错误作为响应

///

array(7) {
  ["raw"]=>
   string(1030) "HTTP/1.1 401 Unauthorized
Date: Fri, 21 Dec 2012 06:24:19 GMT
Status: 401 Unauthorized
Last-Modified: Fri, 21 Dec 2012 06:24:19 GMT
 Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
X-Transaction: a661a742c22b99be
Content-Type: text/html; charset=utf-8
X-MID: 9a96b4c8e27ef14ac86e72706ccd42c44121b507
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.01811
Pragma: no-cache
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Set-Cookie: k=10.35.51.118.1356071059097811; path=/; expires=Fri, 28-Dec-12 06:24:19        GMT; domain=.twitter.com
Set-Cookie: guest_id=v1%3A13560710591072098; domain=.twitter.com; path=/; expires=Sun,   21-Dec-2014 18:24:19 GMT
Set-Cookie:    _twitter_sess=BAh7CCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoK     QHVzZWR7ADoHaWQiJThiOGE1OGQ3NTM4NTNlNTFmMmU1OGM3%250ANjliMDkxZjVkOg9jcmVhdGVkX2F0bCsIpD4ivDsB--c080621b27b01e102bd15dd85e14c984242127f8; domain=.twitter.com; path=/; HttpOnly
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 21

服务器:tfe

 "
   ["headers"]=>
   array(18) {
    ["HTTP/1.1 401 Unauthorized"]=>
    string(0) ""
    ["Date"]=>
    string(29) "Fri, 21 Dec 2012 06:24:19 GMT"
    ["Status"]=>
    string(16) "401 Unauthorized"
    ["Last-Modified"]=>
    string(29) "Fri, 21 Dec 2012 06:24:19 GMT"
    ["Cache-Control"]=>
     string(62) "no-cache, no-store, must-revalidate, pre-check=0, post-check=0"
    ["X-Transaction"]=>
    string(16) "a661a742c22b99be"
    ["Content-Type"]=>
    string(24) "text/html; charset=utf-8"
    ["X-MID"]=>
    string(40) "9a96b4c8e27ef14ac86e72706ccd42c44121b507"
    ["X-Frame-Options"]=>
   string(10) "SAMEORIGIN"
    ["X-Runtime"]=> 
    string(7) "0.01811"
    ["Pragma"]=>
    string(8) "no-cache"
    ["Expires"]=>
   string(29) "Tue, 31 Mar 1981 05:00:00 GMT"
   ["Set-Cookie"]=>
   string(265)   "_twitter_sess=BAh7CCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjo  KQHVzZWR7ADoHaWQiJThiOGE1OGQ3NTM4NTNlNTFmMmU1OGM3%250ANjliMDkxZjVkOg9jcmVhdGVkX2F0bCsIpD4ivDsB--c080621b27b01e102bd15dd85e14c984242127f8; domain=.twitter.com; path=/; HttpOnly"
    ["Vary"]=>
    string(15) "Accept-Encoding"
    ["Content-Encoding"]=>
   string(4) "gzip"
    ["Content-Length"]=>
    string(2) "21"
    ["Server"]=>
    string(3) "tfe"
    [""]=>
    string(0) ""
  }
  ["code"]=>
  int(401)
  ["response"]=>
   string(1) " "
   ["info"]=>
   array(22) {
    ["url"]=>
     string(42) "https://api.twitter.com/oauth/access_token"
     ["content_type"]=>
    string(24) "text/html; charset=utf-8"
    ["http_code"]=>
    int(401)
    ["header_size"]=>
    int(1029)
    ["request_size"]=>
    int(507)
    ["filetime"]=>
    int(-1)
    ["ssl_verify_result"]=>
    int(0)
    ["redirect_count"]=>
    int(0)
    ["total_time"]=>
    float(0.364576)
    ["namelookup_time"]=>
    float(0.002676)
    ["connect_time"]=>
    float(0.079689)
    ["pretransfer_time"]=>
    float(0.255704)
    ["size_upload"]=>
    float(0)
    ["size_download"]=>
    float(21)
    ["speed_download"]=>
    float(57)
    ["speed_upload"]=>
    float(0)
    ["download_content_length"]=>
    float(21)
    ["upload_content_length"]=>
    float(0)
    ["starttransfer_time"]=>
    float(0.364164)
    ["redirect_time"]=>
    float(0)
    ["certinfo"]=>
    array(0) {
    }
    ["request_header"]=>
    string(507) "POST /oauth/access_token HTTP/1.1
User-Agent: tmhOAuth 0.7.2+SSL - //github.com/themattharris/tmhOAuth
Host: api.twitter.com
Accept: */*
Accept-Encoding: deflate, gzip
Authorization: OAuth oauth_consumer_key="*****",
 oauth_nonce="****", oauth_signature="TzcIi0kOwhQ20xRMUQvYYl093B0%3D",         oauth_signature_method="HMAC-SHA1", oauth_timestamp="1356071058",     oauth_verifier="H2gFsotBlUPRBCdLu8ULpaV7fQVmSMGNmIaiWiYPERY", oauth_version="1.0"
Content-Length: 0

" } ["error"]=> string(0) "" ["errno"]=> int(0) }

////

这里有什么问题?

4

2 回答 2

0

您是否确保您的 Twitter 应用程序在 OAuth 中具有读/写权限?这可能会导致“未经授权”的问题

于 2012-12-21T05:34:52.307 回答
0

最后我得到了一些工作。这是会话变量中的问题。$_SESSION['oauth_token'] 和 $_SESSION['oauth_token_secret'] 而我使用了 $_SESSION['otoken'] 和 $_SESSION['osecret']。然后我开始工作了。

于 2012-12-21T11:14:53.790 回答