【问题标题】:Posting to Tumblr with php & Tumblr API使用 php 和 Tumblr API 发布到 Tumblr
【发布时间】:2014-02-16 07:45:24
【问题描述】:

我正在尝试将消息自动发布到我的 Tumblr 博客(将通过 Cron 每天运行)

我在这里使用官方 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,即使您只是在阅读:tumblr.com/docs/en/api/v2(文档令人困惑,因为提到了 none,但没有支持它的端点)。
  • 如果我没记错的话,如果你让你的网站自动发布到 Tumblr 违反了服务条款协议。现在,这并不意味着没有用户这样做,但我相信由于过去的垃圾邮件发送者,这种做法是不鼓励的,它可能会导致您的帐户被暂停。
  • 感谢您的回复...我也发现文档令人困惑(也许正如 Ally 所说,他们将其归类为垃圾邮件,因此不想提供帮助)。我用 Twitters API 管理了同样的事情,这很容易.. 但是 Tumbler :-(
  • 我做过类似的事情,但我停得更远,也许这个实现至少可以帮助你:stackoverflow.com/questions/36747697/…
  • 我也遇到过类似的问题。为了解决它,我遵循了本教程,现在它工作正常! github.com/tumblr/tumblr.php/issues/22

标签: php api oauth cron tumblr


【解决方案1】:
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/

【讨论】:

    【解决方案2】:

    看起来您在代码中删除的部分与所需操作所必需的 OAuth 流程的一部分有关。

    // exchange the verifier for the keys
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      相关资源
      最近更新 更多