3
好吧,我迷上了這個。Tumblr使用PHP的OAuth類的OAuth
我最近下載並安裝了PHP的OAuth庫在這裏:http://us3.php.net/manual/en/book.oauth.php
利用這一點,我創建了一些測試代碼來嘗試連接到我的Tumblr帳戶並崗位。
我能夠獲得RequestToken和Authorize。但是,當我做出實際要求我得到一個錯誤:
Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a redirect)
我的請求URL是在http://api.tumblr.com/v2/blog/account/post正確的格式。我正在發一個POST請求。所以,我不知道爲什麼它返回一個404
這裏是我的代碼:
<?php
$req_url = 'http://www.tumblr.com/oauth/request_token';
$authurl = 'http://www.tumblr.com/oauth/authorize';
$acc_url = 'http://www.tumblr.com/oauth/access_token';
$conskey = '123';
$conssec = '456';
session_start();
// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
$oauth = new OAuth($conskey,$conssec);
$oauth->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$request_token_info = $oauth->getRequestToken($req_url, 'http://www.domain.com/oauth.php');
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
//Make authorize request with oauth_token
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
exit;
} else if($_SESSION['state']==1) {
//Callback code
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
//Make tumblr post request
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$oauth->fetch("http://api.tumblr.com/v2/blog/account/post", null, OAUTH_HTTP_METHOD_POST, array('type'=>'text', 'body'=>'this is a test')); //error happens here
$json = json_decode($oauth->getLastResponse());
print_r($json);
} catch(OAuthException $E) {
print_r($E);
}
?>
任何提示都十分讚賞
也不要忘了在授權獲取調用之前調用setToken方法。 – JacopKane 2012-06-13 12:55:22