使用PHP和Tumblr API发布到Tumblr

17
我正在尝试通过Cron每天自动发布消息到我的Tumblr博客。
我在这里使用官方的Tumblr PHP库: https://github.com/tumblr/tumblr.php 并且使用这里详细介绍的身份验证方法: https://github.com/tumblr/tumblr.php/wiki/Authentication (或其中的部分,因为我不需要用户输入!)
我有以下代码。
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);

然而,这给我带来了以下错误:
致命错误:未捕获的Tumblr\API\RequestException:[401]:未经授权抛出 /home///*/vendor/tumblr/tumblr/lib/Tumblr/API/Client.php 第426行
我可以正常检索博客文章和其他数据(例如):
echo '<pre>';
print_r( $client->getBlogPosts($blogName, $options = null) );
echo '</pre>';

所以看起来只是在发布一个我无法管理的帖子。
老实说,我真的不太理解OAuth身份验证,所以我正在使用一些更有价值的程序员们慷慨提供的免费代码 :-) 我假设我可以编辑掉https://github.com/tumblr/tumblr.php/wiki/Authentication的部分,因为我不需要用户输入,这只是直接从我的服务器上运行的代码(通过Cron)。
我花了几天的时间在互联网上寻找答案(有一点进展),但在这个问题上完全陷入困境...
非常感谢任何建议!

我认为问题出在你删除的OAuth部分。即使你只是阅读Tumblr API v2,也需要OAuth。文档可能有些混乱,因为没有提到“none”,但是没有支持它的终端点。 - mikedidthis
如果我没记错的话,如果你让你的网站自动发布到Tumblr上,那么这将违反服务协议。当然,并不意味着没有用户这样做,但是由于过去的垃圾邮件发送者,这种做法已经不被鼓励,并且可能会导致你的账户被暂停。 - Ally
谢谢大家的回复...我也觉得文档很令人困惑(或许正如Ally所说,他们把这个当做垃圾邮件,所以不愿意提供帮助)。我之前用Twitter的API做了相同的事情,那个相对来说比较容易...但是Tumblr就不一样了 :-( - Ford
我做过类似的事情,但是我停在了更远的地方,也许这个实现至少可以帮助你: http://stackoverflow.com/questions/36747697/oauth-signature-creation-issue-with-php - Matteo Bononi 'peorthyr'
我曾经遇到过类似的问题。为了解决它,我按照这个教程操作,现在它已经正常工作了!https://github.com/tumblr/tumblr.php/issues/22 - Scipius2012
你能告诉我们确切的是哪一行 第426行 或者你的代码中哪一行调用了那一行吗? - Nathan F.
2个回答

0

看起来,您在代码中删除的部分与OAuth过程中的一个必要部分有关,而该部分对所需的操作至关重要。

// exchange the verifier for the keys

您可以尝试运行身份验证示例,并删除已删除的代码部分,直到它不再起作用。这将缩小导致问题的原因范围。我个人对OAuth不是很熟悉,但是看起来这似乎是问题的一部分,因为您删除的主要部分之一是围绕OAuth过程交换验证器以获取OAuth密钥的。

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/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接