2

我在使用 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="mail.user@mail.com"; // 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"}

我该如何解决这个错误?

4

1 回答 1

0

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

  1. 使用我们的 OAuth 流程并让用户使用他们的 Disqus 帐户登录——您不会包含author_nameauthor_email如果您使用此方法
  2. 使用您的网站所有者访问令牌(对客人评论执行此操作,如上面的示例)

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

这是一个如何让用户进行身份验证的示例,然后为您提供访问令牌: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="mail.user@mail.com"; // 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 ...
?>
于 2012-12-17T22:59:24.130 回答