tumblr / tumblr.php

Tumblr API v2 PHP Client
Apache License 2.0
407 stars 115 forks source link

Missing_or_invalid_oauth_verifier_ #58

Closed dimisus closed 9 years ago

dimisus commented 9 years ago

I am trying since hours, I don't get it why the verifier does not work.

Appreciate any help.

CODE:
$consumerKey = '****';
$consumerSecret = '****';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');

// start the old gal up
$resp = $requestHandler->request('POST', 'oauth/request_token', array());

// get the oauth_token
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);

echo '
 RESPONSE '; print_r($out); echo '
'; echo '
 data '; print_r($data); echo '
'; $_SESSION["secretword"] = $data['oauth_token']; $_SESSION["theme"] = $data['oauth_token_secret']; echo 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token']; $client->setToken($data['oauth_token'], $data['oauth_token_secret']); // get the verifier (will be in URL that the user comes back to) // echo "
oauth_verifier: " . $verifier; // $handle = fopen('php://stdin', 'r'); // $line = fgets($handle); // exchange the verifier for the keys $verifier = $_GET["oauth_verifier"]; echo '
next verifier: ' . $verifier; $resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $_GET['oauth_verifier'])); $out = $result = $resp->body; $data = array(); parse_str($out, $data); echo '
 OUT '; print_r($out); echo '
'; echo '
 DATA '; print_r($data); echo '
'; // and print out our new keys $token = $data['oauth_token']; $secret = $data['oauth_token_secret']; echo "
token: " . $token . "
secret: " . $secret; // and prove we're in the money $client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret); $info = $client->getUserInfo(); echo "\ncongrats " . $info->user->name . "!\n"; echo $consumerSecret, '
'; echo $token, '
'; echo $dataecret, '
'; echo $client->getBlogInfo($blogName);
RESPONSE:

RESPONSE Guzzle\Http\EntityBody Object
(
    [contentEncoding:protected] => 
    [rewindFunction:protected] => 
    [stream:protected] => Resource id #25
    [size:protected] => 
    [cache:protected] => Array
        (
            [wrapper_type] => PHP
            [stream_type] => TEMP
            [mode] => w+b
            [unread_bytes] => 0
            [seekable] => 1
            [uri] => php://temp
            [is_local] => 1
            [is_readable] => 1
            [is_writable] => 1
        )

    [customData:protected] => Array
        (
            [default] => 1
        )

)
 data Array
(
    [oauth_token] => 9vkc8pnG1Hzyxs3pVkqbiQ57UvknvQJFCwCTilZHpvLvU5u8I6
    [oauth_token_secret] => 7XoNTknw0U5Iolwx1M1AuVe2lb036RTa6S6XVhwlxyjPxL7A5m
    [oauth_callback_confirmed] => true
)
https://www.tumblr.com/oauth/authorize?oauth_token=9vkc8pnG1Hzyxs3pVkqbiQ57UvknvQJFCwCTilZHpvLvU5u8I6
oauth_verifier: 
next verifier: do2pDHzPzgaJjMpKGUMBqlz1sRY9Rbngz1OPZsz9pEYg0yPSiZ
 OUT Guzzle\Http\EntityBody Object
(
    [contentEncoding:protected] => 
    [rewindFunction:protected] => 
    [stream:protected] => Resource id #29
    [size:protected] => 
    [cache:protected] => Array
        (
            [wrapper_type] => PHP
            [stream_type] => TEMP
            [mode] => w+b
            [unread_bytes] => 0
            [seekable] => 1
            [uri] => php://temp
            [is_local] => 1
            [is_readable] => 1
            [is_writable] => 1
        )

    [customData:protected] => Array
        (
        )

)
 DATA Array
(
    [Missing_or_invalid_oauth_verifier_] => 
)

token: 
secret:
vitor-mariano commented 9 years ago

I didn't understand you code. Try to refactor your code following this doc. https://github.com/matheusmariano/tumblr.php/blob/improving-docs/README.md

ceyko commented 9 years ago

@dimisus I believe you're just missing a trim() on your oauth_verifier. I constructed a more minimal version of your code and can successfully complete the OAuth flow:

// Setup
$client = new Tumblr\API\Client('...','...');
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');

// Request Token
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
$request_tokens = array();
parse_str($resp->body, $request_tokens);
$client->setToken($request_tokens['oauth_token'], $request_tokens['oauth_token_secret']);

// Authorization
echo 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $request_tokens['oauth_token'] . "\n> ";
$stdin_handle = fopen('php://stdin', 'r');
$oauth_verifier = trim(fgets($stdin_handle));

// Access token
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $oauth_verifier));
$access_tokens = array();
parse_str($resp->body, $access_tokens);
$client->setToken($access_tokens['oauth_token'], $access_tokens['oauth_token_secret']);

// Validate
$requestHandler->setBaseUrl('https://api.tumblr.com/');
echo $client->getUserInfo()->user->name . "\n";