【问题标题】:Trouble using PHP Twitter libraries to send tweet使用 PHP Twitter 库发送推文时遇到问题
【发布时间】:2012-05-16 17:42:51
【问题描述】:

首先,我对 PHP 了解不多。但是,我不确定这是一个 PHP 问题,但可能更多的是我理解用于连接到 Twitter 的库的问题。

我按照这里的教程http://140dev.com/twitter-api-programming-tutorials/hello-twitter-oauth-php/ 使用了马特哈里斯的推特库。除了从 twitter 获得的密钥之外,我没有更改任何内容。

当我运行以下代码时,我收到未经授权的错误代码 401:

  $tweet_text = 'Hello Twitter';
  print "Posting...\n";
  $result = post_tweet($tweet_text);
  print "Response code: " . $result . "\n";

 function post_tweet($tweet_text) {

 require_once('tmhoauth/tmhOAuth.php');

 $connection = new tmhOAuth(array(
'consumer_key' => '******',
'consumer_secret' => '******',
'user_token' => '******',
'user_secret' => '******',
)); 

// Make the API call
$connection->request('POST', 
$connection->url('1/statuses/update'), 
array('status' => $tweet_text));

return $connection->response['code'];
 }
?>

正如我之前所说,我使用的是 twitter 提供的密钥。

有人可以帮助我尝试使用上述库来测试https://api.twitter.com/1/account/verify_credentials.json 的 twitter 凭据验证器吗?

我还尝试将以下示例与此 SO 帖子 Using basic oauth to send a tweet 中的 TwitterOAuth 库一起使用:

<?php
require_once('twitteroauth.php');
$connection = new TwitterOAuth('app consumer key', 'app consumer secret', 'my access     token', 'my access token secret');
$connection->post('statuses/update', array('status' => 'text to be tweeted'));

效果也不好;但它确实将页面转发到作者的github页面!?!?

感谢您提供的任何帮助!

【问题讨论】:

    标签: php twitter twitter-oauth


    【解决方案1】:

    如果您确定密钥是正确的,我建议您检查 twitter 中的应用程序设置以确保启用写入模式。

    在应用程序设置下确保应用程序类型访问权限设置为

    读取、写入和访问直接消息

    (或至少读/写)

    那么或许可以尝试再次生成访问令牌?

    【讨论】:

    • 实际上是访问令牌没有适当的权限。我必须删除应用程序的访问权限,然后重新创建访问密钥。显然,如果您在将应用程序设置设置为读取、写入和访问之前请求访问令牌,则令牌将在创建时获得与应用程序相同的访问权限。它不会随着您更改应用程序的权限级别而改变。谢谢!
    • Twitter 应该告诉它,如果您更改权限,您必须再次生成密钥...不过,谢谢您的建议
    猜你喜欢
    • 1970-01-01
    • 2011-11-18
    • 2023-02-11
    • 1970-01-01
    • 2020-02-05
    • 2018-10-14
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多