Disqus API Generates Error Message

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"; // eg, 455718495 β€” you'll need to also create a $forum and pass that if you know only the thread 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, ie, 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, such as api_key, the message you want to comment on, the thread id, mail and username when you run the php code gives me the following error:

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

How can I solve this error?

+2
source share
1 answer

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

  • Use our OAuth stream and let the user log in with their Disqus account - you will not include author_name or author_email if you use this method
  • Use the access token of your site owner (do this for guest comments, as in the above example)

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

Here is an example of how to allow the user to authenticate, and then provide you with an access token: https://github.com/disqus/DISQUS-API-Recipes/tree/master/php/oauth

Once you have purchased the access token, the script should look like this:

 <?php ini_set('display_errors', 'on'); $thread="XXXXXXXXX"; // eg, 455718495 β€” you'll need to also create a $forum and pass that if you know only the thread 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, ie, 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 ... ?> 
0
source

Source: https://habr.com/ru/post/1469328/


All Articles