22279

Disqus API create post error

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?

Answer1:

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:

<ol> <li>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</li> <li>Use your site owner access token (do this for guest comments like in your example above)</li> </ol>

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

Recommend

  • Matching Different Value within One Row
  • cURL - works in browser, not in cURL
  • Way to specify resource's fields list in RESTful API request
  • Solr - DIH define & import many-to-many field
  • Combined Search for Author & Custom Post Type
  • NullPointerException while adding an object to an array
  • rabbitmq-erlang-client, using rebar friendly pkg, works on dev env fails on rebar release
  • How to convert string to table or objects in powershell
  • EventEmitter in shared service's constructor doens't emit data
  • Cypher - matching two different possible paths and return both
  • Scipy Leastsq Optional Output Variable (Mesg)
  • Suppressing passwd when calling sqlplus from shell script
  • JSON response opens as a file, but I can't access it with JavaScript
  • Play WS (2.2.1): post/put large request
  • jQuery .attr() and value
  • DomPDF {PAGE_NUM} not on first page
  • Lost migrations and Azure database is now out of sync
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Azure Cloud Service Web Role web pages do not load
  • Fetching methods from BroadcastReceiver to update UI
  • JavaScriptCore crash on iOS9
  • Symfony2: How to get request parameter
  • Importing jscolor library in angular 2
  • To display the title for the current loaction in map in iphone
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • GridView Sorting works once only
  • Arrays break string types in Julia
  • Traverse Array and Display in markup
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Revoking OAuth Access Token Results in 404 Not Found
  • WPF Applying a trigger on binding failure
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How to set the response of a form post action to a iframe source?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs