首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OAuth类的Tumblr OAuth

使用OAuth类的Tumblr OAuth
EN

Stack Overflow用户
提问于 2012-02-04 03:49:06
回答 1查看 6.1K关注 0票数 3

好了,这件事我被难住了。

我最近在这里下载并安装了php的OAuth库:http://us3.php.net/manual/en/book.oauth.php

在此基础上,我创建了一些测试代码来尝试连接到我的Tumblr帐户并发布帖子。

我可以得到RequestToken并授权。但是当我发出一个实际的请求时,我得到一个错误:

代码语言:javascript
复制
Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a redirect)

我的请求url的格式是正确的http://api.tumblr.com/v2/blog/account/post。我在做一个POST请求。所以我不确定为什么它会返回404。

下面是我的代码:

代码语言:javascript
复制
<?php
$req_url = 'http://www.tumblr.com/oauth/request_token';
$authurl = 'http://www.tumblr.com/oauth/authorize';
$acc_url = 'http://www.tumblr.com/oauth/access_token';

$conskey = '123';
$conssec = '456';

session_start();

// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
  $oauth = new OAuth($conskey,$conssec);
  $oauth->enableDebug();
  if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
    $request_token_info = $oauth->getRequestToken($req_url, 'http://www.domain.com/oauth.php');
    $_SESSION['secret'] = $request_token_info['oauth_token_secret'];
    $_SESSION['state'] = 1;

    //Make authorize request with oauth_token
    header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
    exit;
  } else if($_SESSION['state']==1) {

    //Callback code

    $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
    $access_token_info = $oauth->getAccessToken($acc_url);
    $_SESSION['state'] = 2;
    $_SESSION['token'] = $access_token_info['oauth_token'];
    $_SESSION['secret'] = $access_token_info['oauth_token_secret'];

  } 

  //Make tumblr post request
  $oauth->setToken($_SESSION['token'],$_SESSION['secret']);
  $oauth->fetch("http://api.tumblr.com/v2/blog/account/post", null, OAUTH_HTTP_METHOD_POST, array('type'=>'text', 'body'=>'this is a test'));  //error happens here
  $json = json_decode($oauth->getLastResponse());
  print_r($json);
} catch(OAuthException $E) {
  print_r($E);
}
?>

任何提示都是非常感谢的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-04 20:25:53

我已经找到了答案。

问题出在这里:

代码语言:javascript
复制
$oauth->fetch("http://api.tumblr.com/v2/blog/account/post", null, OAUTH_HTTP_METHOD_POST, array('type'=>'text', 'body'=>'this is a test'));  //error happens here

首先,为了方便起见,我刚刚给我的博客起了个名字。实际上,tumblr api中描述的base-hostname需要采用‘blogname.tomblr.com’的格式。而不是我所说的“博客名”。这会给出400分。

其次,获取参数的顺序错误。正确的顺序是:

代码语言:javascript
复制
string $protected_resource_url [, array $extra_parameters [, string $http_method [, array $http_headers ]]] 

在我的例子中是这样的:

代码语言:javascript
复制
$oauth->fetch("http://api.tumblr.com/v2/blog/blogname.tumblr.com/post", array('type'=>'text', 'body'=>'this is a test'), OAUTH_HTTP_METHOD_POST );

我写了一篇关于如何让它工作的教程here

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

https://stackoverflow.com/questions/9134617

复制
相关文章

相似问题

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