【问题标题】:"Bad Request" response on Tumblr's API "quote" request对 Tumblr 的 API“报价”请求的“错误请求”响应
【发布时间】:2013-08-13 19:22:45
【问题描述】:

我开发了一个使用 Tumblr API 的 PHP 脚本。当我发布“文本”时,一切正常,但在“引用”帖子上,我收到“错误请求”错误。我使用Composerhttps://github.com/tumblr/tumblr.php 获取“tumblr/tumblr”存储库。

这是我的 PHP 脚本:

<?php

require 'vendor/autoload.php';

$consumer_key = 'KEY';
$consumer_secret = 'SECRET';
$token = 'TOKEN';
$token_secret = 'SECRET';
$blog = 'BLOG';

$client = new Tumblr\API\Client($consumer_key, $consumer_secret, $token, $token_secret);

#$options = array('type' => 'text', 'title' => 'Title', 'body' => 'Body', 'tags' => 'Test');
$options = array('type' => 'quote', 'text' => 'Text', 'source' => 'Source', 'tags' => 'Test');

try{
    $res = $client->createPost($blog, $options);
}
catch(Exception $e){
    print "ERROR: ".$e->getMessage()."\n";
    exit(1);
}

我该如何解决这个问题?

【问题讨论】:

  • 使用 API 检查器工具(如 Fiddler、Charles 或 Runscope)来查看来回的实际 HTTP 请求/响应。这将有助于缩小范围。

标签: php api tumblr


【解决方案1】:

documentation 开始,引用类型需要 "quote" 字段而不是 "text" 字段。

将您的参数更改为此,它应该可以工作:

$options = array('type' => 'quote', 'quote' => 'Text', 'source' => 'Source', 'tags' => 'Test');

当然,Tumblr\API\RequestException 似乎没有返回有用的错误消息。我可以通过破解Tumblr\API\RequestException 并将var_dump 放入构造函数中来解决这个问题:

public function __construct($response)
{
    $error = json_decode($response->body);
    var_dump($error->response);
    ...

这样,我至少能够得到一个很好的错误信息:

class stdClass#668 (1) {
  public $errors =>
  array(1) {
    [0] =>
    string(21) "Post cannot be empty."
  }
}

【讨论】:

  • 天哪,当然可以。非常感谢!!!有用! :) 如果您没有捕获异常,它只会返回有用的错误消息。
猜你喜欢
  • 2021-01-07
  • 2016-05-24
  • 2014-08-19
  • 2018-10-26
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
相关资源
最近更新 更多