2012-12-16 30 views
1

我有一個問題,使用Disqus API嘗試評論在tumblr發表的出版物。 這是代碼:Disqus API創建帖子錯誤

<?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="[email protected]"; // 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"} 

我該如何解決這個錯誤?

回答

0

默認情況下,Disqus API應用程序現在設置爲使用OAuth,因此您必須爲此示例添加一個'access_token'參數。有兩種方法來獲得一個訪問令牌:

  1. 使用我們的OAuth流,讓用戶登錄他們的帳戶Disqus - 你將不包括author_nameauthor_email如果您使用此方法
  2. 使用您的網站所有者的訪問令牌(這樣做對客人的意見就像在你上面的例子)

這裏的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="[email protected]"; // 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

你不能使用access_token和author_ *參數,因爲如果您使用的是acces_token,您會將評論發佈爲自己的內容 – Fky