使用 PHP 的 OAuth 类的 Tumblr OAuth

发布于 01-02 20:59 字数 2244 浏览 5 评论 0原文

好吧,我被这个问题难住了。

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

使用它,我创建了一些测试代码来尝试连接到我的 Tumblr 帐户并发帖。

我能够获取 RequestToken 并授权。但是,当我发出实际请求时,出现错误:

Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a redirect)

我的请求网址的格式正确为 http://api.tumblr.com/v2/blog/account/post。我正在发出 POST 请求。所以我不确定为什么它返回 404。

这是我的代码:

<?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);
}
?>

非常感谢任何提示

Allright, I'm stumped on this one.

I recently downloaded and installed php's OAuth library here: https://www.php.net/manual/en/book.oauth.php

Using this, I created some test code to try to connect to my Tumblr account and make a post.

I am able to get the RequestToken and Authorize. But when I make an actual request I get an error:

Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a redirect)

My request url is in the correct format of http://api.tumblr.com/v2/blog/account/post. And I'm making a POST request. So I am not sure why it's returning a 404.

Here is my code:

<?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);
}
?>

Any hints are greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍2025-01-09 20:59:26

我已经找到答案了。

问题就出在这里:

$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 中描述的基本主机名需要采用“blogname.tumblr.com”格式。不仅仅是我所拥有的“博客名称”。这将给出 400。

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

string $protected_resource_url [, array $extra_parameters [, string $http_method [, array $http_headers ]]] 

翻译成我的例子是......

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

我写了一个关于让它工作的教程 此处

I have found the answer.

The problem lies here:

$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

First of all, for account, I just had the name of my blog. In fact, the base-hostname as described in the tumblr api needs to be in the format 'blogname.tumblr.com'. Not just 'blogname' as I had it. This will give a 400.

Secondly, the fetch parameters were in the wrong order. The correct order is this:

string $protected_resource_url [, array $extra_parameters [, string $http_method [, array $http_headers ]]] 

Which in translation to my example would be...

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

I wrote a tutorial on getting this to work here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文