4
votes

I'm trying to use the Disqus API to add a post to an existing discussion/forum. In the documentation I can read that I can send a comment without authentication, as a guest. The documentation says this: http://disqus.com/api/docs/posts/create/

Anonymous comments are allowed under two conditions:

  1. You're using legacy auth, and your secret key
  2. You're using your public key, you've come from a verified referrer, you're unauthenticated, and the forum you're attempting to create the post on is listed in the applications trusted forums.

To create an anonymous comment, simply pass author_email and author_name, and optionally the author_url parameter.

So I use this code to create a comment in PHP. (I use a very simple cURL class but the problem isn't there because I get the same one in the console on disqus.com/api)

$curl = new Curl(1); 

$curl->addPostVar('thread','THREAD_ID');
$curl->addPostVar('message','Text message');
$curl->addPostVar('author_email','My email');
$curl->addPostVar('author_name','My name');
$curl->addPostVar('api_secret','My application secret API key');

echo $curl->exec('https://disqus.com/api/3.0/posts/create.json');

But I get the error through JSON

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

I know someone else has already asked this (Disqus API create post error) but the response suggested was to use OAuth and get authenticated. But I don't want to authenticate, I want to send a guest comment with name and email. Where I'm wrong?

Thank you very much for any replies.

3

3 Answers

3
votes

To comment anonymously you would not pass any access tokens. However, you have to make sure the following conditions are true:

  1. In your website settings, under General, make sure "Allow Guest Comments" is checked
  2. In your Disqus website settings, under the Advanced tab, add your domain(s) as trusted domains
  3. In your API application settings, add the same domains from your trusted domains to the API application's trusted domains list
  4. When you make the API request to post the comment, make sure your "referer" header had a domain which is on one of those trusted domains, and set the "host" header to '.disqus.com' for good measure
  5. Make the API request using your public key

I was able to make the request successfully following those rules.

Just to make it clear, you would need to own/moderate the site in order to post anonymous comments to it through the API. Also make sure you pass an additional strict=1 argument when you do this. By default any optional arguments that error are simply dropped, so you can potentially run into the situation where comments post as yourself if author_name and author_email throw an error.

2
votes

After checking the obvious things (like enabling guest posting and checking my referring domains in the forum and application settings), I was finally able to solve this using the disqus-php library:

require __DIR__ . '/disqus-php-master/disqusapi/disqusapi.php';

$disqus = new DisqusAPI($secret_key);

print_r($disqus->posts->create(array(
    'thread' => $thread_id,
    'message' => $message,
    'author_name' => $author_name,
    'author_email' => $author_email,
    'api_key' => $api_key,
)));

The catch is that the api_key is NOT the same thing as the public key shown in your Disqus application settings. I actually had to inspect one of the AJAX calls from the Disqus Javascript widget to get the correct api_key:

Disqus AJAX call headers showing the api_key

1
votes

I open up this thread again because I have the same question and it seems there no answer to it yet.

I have been trying for a few days now to make a comments through the API as a guest. If I read the API documentation it say I have to pass the message, author_name, author_url, thread and api_key to do that, but can't do it. I always gets

"Code: 12"

and

"This application cannot create posts on the chosen forum".

If I send through the access_token for it works, but then it will be posted as me and thats not I want.

I also found Disqus recipes on Github and in the readme file in Beginner section it's refers to a snippet

"Create a guest comment: /php/create-guest-comment.php"

but can't find the snippet. So I look through the commits and find "Delete obsolete guest comment creation script". Should I take that as a hint that Disqus for now don't can create a comments as a guest through the API? If so Disqus may update your documentation.

If I'm doing something wrong I would appreciate you point me in right direction.