首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用php & Tumblr API发布到Tumblr

使用php & Tumblr API发布到Tumblr
EN

Stack Overflow用户
提问于 2014-01-25 06:30:48
回答 2查看 8.7K关注 0票数 14

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

我在这里使用官方的Tumblr库:https://github.com/tumblr/tumblr.php

并使用这里详细介绍的身份验证方法:https://github.com/tumblr/tumblr.php/wiki/Authentication (或其中的一部分,因为我不需要用户输入!)

我有下面的代码

代码语言:javascript
运行
复制
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);

但是,这给我带来了以下错误:

致命错误: Uncaught \API\RequestException: 401:在第426行的/home///*/vendor/tumblr/tumblr/lib/Tumblr/API/Client.php中抛出未授权

我可以通过(示例)检索博客文章和其他数据:

代码语言:javascript
运行
复制
echo '<pre>';
print_r( $client->getBlogPosts($blogName, $options = null) );
echo '</pre>';

所以看起来这只是做一个我无法管理的帖子。

老实说,我并不真正理解OAuth身份验证,所以我正在使用更有价值的程序员免费提供的代码:-)我认为我可以编辑掉https://github.com/tumblr/tumblr.php/wiki/Authentication的部分内容,因为我不需要用户输入,因为这只是直接从我的服务器上运行的代码(通过Cron)

我花了几天时间在互联网上寻找一些答案(得到了一些更多的答案),但我完全被困在了这个问题上。

任何建议都非常感谢!

EN

回答 2

Stack Overflow用户

发布于 2018-03-28 20:46:07

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

代码语言:javascript
运行
复制
// exchange the verifier for the keys

您可以尝试运行身份验证示例本身,并删除您删除的代码的部分,直到它不再工作为止。这将缩小导致这一问题的原因。我个人对OAuth并不十分熟悉,但这似乎是问题的一部分,因为您提取的主要部分之一是围绕OAuth进程,将验证器替换为OAuth密钥。

票数 0
EN

Stack Overflow用户

发布于 2018-05-16 09:30:15

代码语言:javascript
运行
复制
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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21347513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档