12

I am trying to post messages automatically to my Tumblr Blog (which will run daily via Cron)

I am using the Official Tumblr PHP library here: https://github.com/tumblr/tumblr.php

And using the Authentication method detailed here : https://github.com/tumblr/tumblr.php/wiki/Authentication (or parts of this, as I don't need user input!)

I have the below code

require_once('vendor/autoload.php');

// some variables that will be pretttty useful
$consumerKey = 'MY-CONSUMER-KEY';
$consumerSecret = 'MY-CONSUMER-SECRET';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$blogName = 'MY-BLOG-NAME';
$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);

// set the token
$client->setToken($data['oauth_token'], $data['oauth_token_secret']);

// change the baseURL so that we can use the desired Methods
$client->getRequestHandler()->setBaseUrl('http://api.tumblr.com');

// build the $postData into an array
$postData = array('title' => 'test title', 'body' => 'test body');

// call the creatPost function to post the $postData
$client->createPost($blogName, $postData);

However, this gives me the following error:

Fatal error: Uncaught Tumblr\API\RequestException: [401]: Not Authorized thrown in /home///*/vendor/tumblr/tumblr/lib/Tumblr/API/Client.php on line 426

I can retrieve blog posts and other data fine with (example):

echo '<pre>';
print_r( $client->getBlogPosts($blogName, $options = null) );
echo '</pre>';

So it seems it is just making a post that I cant manage.

In all honesty, I don't really understand the OAuth Authentication, so am using code that more worthy coders have kindly provided free :-) I assume I am OK to have edited out parts of the https://github.com/tumblr/tumblr.php/wiki/Authentication as I don't need user input as this is just going to be code ran directly from my server (via Cron)

I have spent days looking around the internet for some answers (have gotten a little further), but am totally stuck on this one...

Any advice is much appreciated!

Ford
  • 487
  • 6
  • 19
  • I think the issue is with the OAuth parts you have removed. Tumblr API v2 requires OAuth, even if you are just reading: http://www.tumblr.com/docs/en/api/v2 (the docs are confusing as *none* is mentioned, but there are no end points that support it). – mikedidthis Jan 27 '14 at 20:49
  • If I recall correctly, if you have your website post automatically to Tumblr in violates the Terms of Service agreement. Now, that doesn't mean there aren't users who do so, but I believe the practice is discouraged thanks to past SPAM-ers and it could get your account suspended. – Ally Jan 27 '14 at 21:31
  • Thanks for the replies... i am also finding the docs confusing (maybe as Ally says, they class it as spam so dont want to help). i managed the same thing with Twitters API and that was reletevly easy.. but Tumbler :-( – Ford Feb 01 '14 at 03:28
  • I've done something similar, but I stopped a little more further, maybe this implementation at least could help you: http://stackoverflow.com/questions/36747697/oauth-signature-creation-issue-with-php – Matteo Bononi 'peorthyr' Apr 26 '16 at 06:39
  • I have had a similar issue. To resolve it I have followed this tutorial and now it works fine! https://github.com/tumblr/tumblr.php/issues/22 – Scipius2012 Oct 02 '16 at 10:30
  • Could you tell us exactly which line `line 426` is or which line of your code is calling that line? – Nathan F. Mar 28 '18 at 20:46

2 Answers2

0

It looks like the parts that you removed in the code pertained to a portion of the OAuth process that was necessary for the desired action.

// exchange the verifier for the keys

You might try running the Authentication Example itself and removing the parts of the code that you've removed until it no longer works. This will narrow down what's causing the issue. I'm not very familiar with OAuth personally, but this looks as though it would be apart of the problem as one of the main portions you took out was surrounding the OAuth process exchanging the verifier for the OAuth keys.

Nathan F.
  • 2,575
  • 2
  • 23
  • 55
0
function upload_content(){
// Authorization info
$tumblr_email    = 'email-address@host.com';
$tumblr_password = 'secret';
// Data for new record
$post_type  = 'text';
$post_title = 'Host';
$post_body  = 'This is the body of the host.';
// Prepare POST request
$request_data = http_build_query(
    array(
        'email'     => $tumblr_email,
        'password'  => $tumblr_password,
        'type'      => $post_type,
        'title'     => $post_title,
        'body'      => $post_body,
        'generator' => 'API example'
    )
);
// Send the POST request (with cURL)
$c = curl_init('api.tumblr.com/v2/blog/gurjotsinghmaan.tumblr.com/post');  
//api.tumblr.com/v2/blog/{base-hostname}/post       
//http://www.tumblr.com/api/write       
//http://api.tumblr.com/v2/blog/{base-hostname}/posts/text?api_key={}
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
    echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
    echo 'Bad email or password';
} else {
    echo "Error: $result\n";
}

}

https://howtodofor.com/how-to-delete-tumblr-account/