Disqus API创建帖子错误

时间:2012-12-16 01:10:24

标签: php disqus

我在使用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"}

我该如何解决这个错误?

1 个答案:

答案 0 :(得分:0)

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

  1. 使用我们的OAuth流程,让用户使用他们的Disqus帐户登录 - 如果您使用此方法,则不会包含author_nameauthor_email
  2. 使用您的网站所有者访问令牌(对上面示例中的访客评论执行此操作)
  3. 以下是关于OAuth的文档:Disqus http://disqus.com/api/docs/auth/ - 请注意,网站所有者的访问令牌可以在您的应用程序概述中找到:http://disqus.com/api/applications/

    以下是如何让用户进行身份验证,然后为您提供访问令牌的示例: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 ...
    ?>