Disqus API 创建帖子错误

2024-04-28

我在使用 Disqus API 尝试对 tumblr 中发布的出版物发表评论时遇到问题。 这是代码:

<?php
    ini_set('display_errors', 'on');

    $thread="XXXXXXXXX"; // e.g., 455718495 — you'll need to also create a $forum and pass that if you know only the thread's URL or identifier rather than the ID
    $api="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Generate one at http://disqus.com/api/applications/ -- Secret key is required for anonymous comment posting
    $message="Hello world."; // this is the content of the comment, i.e., what you'd normally type in the postbox
    $author_email="[email protected] /cdn-cgi/l/email-protection"; // optional, including this will still make the comment a guest comment, but it will now be claimable 
    $author_name="user"; // optional, can be any display name you like
    $fields_string=""; // DO NOT EDIT

    // set POST variables
    $url = 'http://disqus.com/api/3.0/posts/create.json'; // full documentation at http://disqus.com/api/docs/posts/create/
    $fields = array(
        'api_secret'=>urlencode($api), // change to api_key when using a public key
        'thread'=>urlencode($thread),
        'message'=>urlencode($message),
        'author_email'=>urlencode($author_email),
        'author_name'=>urlencode($author_name),
    );

    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    // execute POST
    $result = curl_exec($ch);

    // close connection
    curl_close($ch);

?>

当您运行代码时,我会传递所需的值,如 api_key、您想要评论的消息、线程 ID、邮件和用户名 php 给我以下错误:

{"code": 4, "response": "You must be authenticated to perform this action"}

我该如何解决这个错误?


默认情况下,Disqus API 应用程序现在设置为使用 OAuth,因此您必须为此示例添加“access_token”参数。有两种获取访问令牌的方法:

  1. 使用我们的 OAuth 流程并让用户使用其 Disqus 帐户登录 - 您不会包括author_name or author_email如果你使用这个方法
  2. 使用您的网站所有者访问令牌(针对访客评论执行此操作,如上面的示例所示)

以下是有关 OAuth 与 Disqus 配合使用的文档:http://disqus.com/api/docs/auth/ http://disqus.com/api/docs/auth/-- 请注意,站点所有者的访问令牌可以在您的应用程序概述中找到:http://disqus.com/api/applications/ http://disqus.com/api/applications/

以下是如何让用户进行身份验证,然后为您提供访问令牌的示例:https://github.com/disqus/DISQUS-API-Recipes/tree/master/php/oauth https://github.com/disqus/DISQUS-API-Recipes/tree/master/php/oauth

获得访问令牌后,脚本应如下所示:

<?php
    ini_set('display_errors', 'on');

    $thread="XXXXXXXXX"; // e.g., 455718495 — you'll need to also create a $forum and pass that if you know only the thread's URL or identifier rather than the ID
    $api="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Generate one at http://disqus.com/api/applications/ -- Secret key is required for anonymous comment posting
    $message="Hello world."; // this is the content of the comment, i.e., what you'd normally type in the postbox
    $author_email="[email protected] /cdn-cgi/l/email-protection"; // optional, including this will still make the comment a guest comment, but it will now be claimable 
    $author_name="user"; // optional, can be any display name you like
    $fields_string=""; // DO NOT EDIT
    $access_token="YOUR_ACCESS_TOKEN";

    // set POST variables
    $url = 'http://disqus.com/api/3.0/posts/create.json'; // full documentation at http://disqus.com/api/docs/posts/create/
    $fields = array(
        'api_secret'=>urlencode($api), // change to api_key when using a public key
        'thread'=>urlencode($thread),
        'message'=>urlencode($message),
        'author_email'=>urlencode($author_email),
        'author_name'=>urlencode($author_name),
        'access_token'=>urlencode($access_token),
    );

    // rest of script ...
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Disqus API 创建帖子错误 的相关文章

  • PHP Simple Html Dom 获取div的纯文本,但避免所有其他标签

    我使用 PHP Simple Html Dom 来获取一些 html 现在我有一个像下面的代码一样的 html dom 我需要获取纯文本内部 div 但避免 p 标签及其内容 仅返回 111111 谁可以帮助我 谢谢提前 div p 000
  • 使用操作系统用户名/密码在 PHP 中进行身份验证?

    我想知道是否有一种方法可以使用 PHP 或者可能是其他一些 CGI 但最好是 PHP 来使用其操作系统 在本例中为 Linux Debian 5 用户名和密码来验证用户 可能的 难的 谢谢 一般来说 这种东西是特定于操作系统的 并与现有的身
  • 如何使用 zend 导入 CSV

    如何使用 zend 框架导入 CSV 文件 我应该使用 zend file transfer 还是有任何我必须研究的特殊类 另外 如果我使用 zend file transfer 是否有任何特殊的 CSV 验证器 你不必使用任何 zend
  • 如何让 PagerDefault 查询与 Drupal 7 一起正常工作?

    我正在运行以下代码 query db select taxonomy index ti gt fields ti array nid gt condition ti tid term gt tid gt condition n status
  • Facebook“喜欢”按钮,喜欢另一个页面/URL

    我有一个链接列表 每个单独的页面上都有一个 喜欢 按钮来 喜欢 该页面 但我想要列表中每个链接旁边有一个 喜欢 按钮 该按钮会喜欢旁边链接的 URL 就像单击链接然后单击 喜欢 按钮一样 我该怎么做呢 要在每个单独的页面上创建按钮 我使用以
  • 图像上的文字

    是否可以在 php 中动态地将文本放置在图像上 然后将其发送到 rss feed 是的 可以使用GD http www php net gd函数或图像魔术师 http www php net imagemagick功能 具体取决于您的服务器
  • 如何在 PHP 中进行静态代码分析? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 有没有PHP源文件的静态分析工具 二进制文件本身可以检查语法错误 但我正在寻找功能更多的东西 例如 未使用的变量赋值 未先初始化就分配到的
  • 合并两个数字键关联数组并保留原始键

    我有两个这样的数组 array 11 gt 11 22 gt 22 33 gt 33 44 gt 44 array 44 gt 44 55 gt 55 66 gt 66 77 gt 77 我想组合这两个数组 使其不包含重复项并保留其原始键
  • php 验证整数[重复]

    这个问题在这里已经有答案了 我想知道为什么这不起作用 echo gettype GET id returns string if is int GET id echo Integer 如何验证从 GET POST 传递的数据是否为整数 Ca
  • 如何在 Doctrine 中使用 andWhere 和 orWhere ?

    WHERE a 1 AND b 1 Or b 2 AND c 1 OR c 2 我怎样才能在教义中做到这一点 q gt where a 1 q gt andWhere b 1 q gt orWhere b 2 q gt andWhere c
  • 如何重定向(标头位置)到 html 文件?

    所以我正在 php 中运行一个注册 登录系统 包括 MySQL 出于测试目的 成功登录后 我将用户重定向到 index php 该文件声明用户已登录 并为他提供注销选项 同时 我制作了一个计划使用的实际 html 页面 因此我只是添加了 i
  • 使用 PHP DOMDocument 更改标签属性值

    我想用 PHP DOMDocument 更改标签属性的值 例如 假设我们有这行 HTML a href http foo bar Click here a 我将上面的代码加载到 PHP 中 如下所示 dom new domDocument
  • 我们在哪里/什么时候使用 JSON?

    你能告诉我 JSON 有什么用 在 Javascript 和 PHP 中 当我们需要 JSON 方法时 我从以下链接中阅读 但是 我没有获得有关任何项目的 JSON 实现的任何信息 http www json org js html htt
  • PHP:将数组添加在一起

    有人可以帮我解释一下吗 我有两段代码 其中一个按我的预期工作 但另一个则不然 这有效 a array a gt 1 b gt 2 b array c gt 3 c a b print r c Output Array a gt 1 b gt
  • laravel 5:找不到类“输入”

    In my routes php我有的文件 Route get function return view login Route get index function return view index Route get register
  • php echo 不工作

    我的代码似乎不起作用 单选按钮出现 但旁边什么也没有 似乎 mysql fetch array 由于某种原因无法工作 因为我已经玩过代码并反复测试它以查找代码似乎遇到的位置出现问题并停止工作 有人可以告诉我出了什么问题吗 欢呼声我是新手 最
  • 使用 Laravel dusk 仅迁移一次

    根据到 数据库测试 文档 https laravel com docs 5 4 database testing resetting the database after each test我可以在每次测试后重置数据库 第一个选项 第二个选
  • cakephp 3.0 如何使用值而不是 id 填充选择字段

    我一直在寻找以前的答案 但我找到的答案与旧的 cakephp 版本有关 我有两个表 杂志 和 问题 其中存在关系 问题 属于 杂志 问题表如下所示 public function initialize array config this g
  • PHP 启动:运行单元测试时无法加载动态库

    当我尝试运行单元测试时 出现此错误 PHP 警告 PHP 启动 无法加载动态库 bz2 尝试过 xampp php ext bz2 找不到指定的模块 xampp php ext php bz2 dll 找不到指定的模块 在未知的第 0 行
  • 未加载 Firebird 扩展

    我正在尝试在 PHP 中使用 Firebird 扩展 我有扩展文件 php pdo firebird dll php interbase dll in my ext文件夹 我将它们添加到我的php ini像这样文件 PHP PDO FIRE

随机推荐