由于微博API要求从2011年12月26日起,所有请求必须加入请求者的IP。由于从内网发起请求,最后header中获得的IP为10打头的内网IP,导致认证失败。

修改sae的SaeTClientV2类的http()方法中检测下,如果IP是10打头的内网IP,强制转换header中的saeremoteip为一个外网IP。

$remote_addr = $_SERVER['REMOTE_ADDR'];
        if (substr($remote_addr,0,3) === '10.') {
            $remote_addr = '61.135.152.203';
            $headers[] = "SaeRemoteIP: " . $remote_addr;
        }

错误调试信息:

=====post data====== string(203) "client_id=759886780&client_secret=856def108f655da6fc66ef9d785c&grant_type=authorization_code&code=3c092be334e7325819514ebcdf392911&redirect_uri=http%3A%2F%2F2.anlianni.sinaapp.com%2FIndex%2Fget_token" 
=====headers====== Array ( ) 
=====request info===== Array ( [url] => https://api.weibo.com/oauth2/access_token [content_type] => text/html [http_code] => 604 [header_size] => 150 [request_size] => 606 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.001168 [namelookup_time] => 1.5E-5 [connect_time] => 0.000142 [pretransfer_time] => 0.000171 [size_upload] => 0 [size_download] => 462 [speed_download] => 395547 [speed_upload] => 0 [download_content_length] => 462 [upload_content_length] => 0 [starttransfer_time] => 0.00111 [redirect_time] => 0 [request_header] => POST / HTTP/1.0 User-Agent: Sae T OAuth2 v0.1 Host: fetchurl.sae.sina.com.cn Accept: */* Accept-Encoding: deflate, gzip SaeRemoteIP: 10.215.12.205 SaeAppName: anlianni AccessKey: n205w31254 FetchUrl: https://api.weibo.com/oauth2/access_token TimeStamp: 1331114360 Signature: K0S7UmiZwbPoS3dFlZt1t4x9hwha74SFFS7CHegM58g= Content-Length: 203 Content-Type: application/x-www-form-urlencoded client_id=759886780&client_secret=856def108f655da6fc66ef9d785c&grant_type=authorization_code&code=3c092be334e7325819514ebcdf392911&redirect_uri=http%3A%2F%2F2.anlianni.sinaapp.com%2FIndex%2Fget_token )
 =====response=====

 

SAE ERROR: REST ERROR

 

object(OAuthException)#5 (7) {
  ["message":protected] => string(24) "get access token failed."
  ["string":"Exception":private] => string(0) ""
  ["code":protected] => int(0)
  ["file":protected] => string(68) "/data1/www/htdocs/615/anlianni/2/love/Lib/Common/saetv2.ex.class.php"
  ["line":protected] => int(203)
  ["trace":"Exception":private] => array(7) {
    [0] => array(6) {
      ["file"] => string(70) "/data1/www/htdocs/615/anlianni/2/love/Lib/Action/IndexAction.class.php"
      ["line"] => int(124)
      ["function"] => string(14) "getAccessToken"
      ["class"] => string(11) "SaeTOAuthV2"
      ["type"] => string(2) "->"
      ["args"] => array(2) {
        [0] => string(4) "code"
        [1] => array(2) {
          ["code"] => string(32) "3c092be334e7325819514ebcdf392911"
          ["redirect_uri"] => string(45) "http://2.anlianni.sinaapp.com/Index/get_token"
        }
      }
    }
    [1] => array(4) {
      ["function"] => string(9) "get_token"
      ["class"] => string(11) "IndexAction"
      ["type"] => string(2) "->"
      ["args"] => array(0) {
      }
    }
    [2] => array(4) {
      ["file"] => string(64) "/data1/www/htdocs/615/anlianni/2/ThinkPHP/Lib/Core/App.class.php"
      ["line"] => int(128)
      ["function"] => string(14) "call_user_func"
      ["args"] => array(1) {
        [0] => &array(2) {
          [0] => &object(IndexAction)#2 (2) {
            ["view":protected] => object(View)#3 (1) {
              ["tVar":protected] => array(0) {
              }
            }
            ["name":"Action":private] => string(0) ""
          }
          [1] => string(9) "get_token"
        }
      }
    }
    [3] => array(6) {
      ["file"] => string(64) "/data1/www/htdocs/615/anlianni/2/ThinkPHP/Lib/Core/App.class.php"
      ["line"] => int(155)
      ["function"] => string(4) "exec"
      ["class"] => string(3) "App"
      ["type"] => string(2) "::"
      ["args"] => array(0) {
      }
    }
    [4] => array(6) {
      ["file"] => string(84) "/data1/www/htdocs/615/anlianni/2/ThinkPHP/Extend/Engine/Sae/Lib/Core/Think.class.php"
      ["line"] => int(47)
      ["function"] => string(3) "run"
      ["class"] => string(3) "App"
      ["type"] => string(2) "::"
      ["args"] => array(0) {
      }
    }
    [5] => array(6) {
      ["file"] => string(63) "/data1/www/htdocs/615/anlianni/2/ThinkPHP/Extend/Engine/Sae.php"
      ["line"] => int(72)
      ["function"] => string(5) "Start"
      ["class"] => string(5) "Think"
      ["type"] => string(2) "::"
      ["args"] => array(0) {
      }
    }
    [6] => array(4) {
      ["file"] => string(42) "/data1/www/htdocs/615/anlianni/2/index.php"
      ["line"] => int(9)
      ["args"] => array(1) {
        [0] => string(63) "/data1/www/htdocs/615/anlianni/2/ThinkPHP/Extend/Engine/Sae.php"
      }
      ["function"] => string(7) "require"
    }
  }
  ["previous":"Exception":private] => NULL
}