Disqus API create post error

被刻印的时光 ゝ 提交于 2019-12-10 23:57:28

问题


I have a problem using the Disqus API to try to comment on a publication made ​​in tumblr. This is the code:

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

?>

I pass the required values ​​as are the api_key, the message you wish to comment, the thread ID, the mail and username, when you run the code php gives me the following error:

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

how can i solve this error?


回答1:


By default, Disqus API applications are now set to use OAuth so you have to add an 'access_token' argument to this example. There are two ways to get an the access token:

  1. Use our OAuth flow and let the user log in with their Disqus account -- you won't include author_name or author_email if you use this method
  2. Use your site owner access token (do this for guest comments like in your example above)

Here's the documentation on OAuth works with Disqus: http://disqus.com/api/docs/auth/ -- note that the site owner's access token can be found in your application overview here: http://disqus.com/api/applications/

Here's an example of how to let a user authenticate, and then give you an access token: https://github.com/disqus/DISQUS-API-Recipes/tree/master/php/oauth

Once you've acquired an access token, the script should look like this:

<?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 ...
?>


来源:https://stackoverflow.com/questions/13897710/disqus-api-create-post-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!