2012-02-03 9 views
3

大丈夫、私はこの1つに困っています。Tumblr OAuth PHPの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); 
} 
?> 

任意のヒントを大幅に私は答えを見つけた

答えて

8

を高く評価しています。

問題はここにある:すべての

$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 

まず、アカウントのために、私はちょうど私のブログの名前を持っていました。実際、tumblr apiで説明されているベースホスト名は 'blogname.tumblr.com'の形式である必要があります。私が持っていただけではなく、 "blogname"。これにより、400が返されます。

第2に、フェッチパラメータの順序が間違っていました。 ...

$oauth->fetch("http://api.tumblr.com/v2/blog/blogname.tumblr.com/post", array('type'=>'text', 'body'=>'this is a test'), OAUTH_HTTP_METHOD_POST); 

だろう、私はhere

+0

もすることを忘れないでください動作するようにこれを得ることにチュートリアルを書いた私の例への変換で

string $protected_resource_url [, array $extra_parameters [, string $http_method [, array $http_headers ]]] 

:正しい順序はこれです許可されたフェッチ呼び出しの前にsetTokenメソッドを呼び出します。 – JacopKane

関連する問題