disqus oauth2登录问题

时间:2015-08-05 13:28:01

标签: php disqus

我正在尝试使用disqus登录:http://beta.perfectquiver.com/include/disqus/index.php

我也在disqus配置中设置了重定向URL:http://beta.perfectquiver.com/include/disqus/index.php

但它显示我的错误

Invalid parameter: redirect_uri (values for POST and GET arguments differ)" ["error"]=> string(13) "invalid_grant" }

google之后,有人说这是重定向网址不匹配的问题,它应该是相同的。 但这里它已经相同了。 还是一样的错误。

这里是我正在使用的代码。

$PUBLIC_KEY = "PUBLIC_KEY";
$SECRET_KEY = "SECRET_KEY";
$redirect = urlencode("http://beta.perfectquiver.com/include/disqus/index.php");


$endpoint = 'https://disqus.com/api/oauth/2.0/authorize?';
$client_id = $PUBLIC_KEY;
$scope = 'read,write';
$response_type = 'code';

$auth_url = $endpoint.'&client_id='.$client_id.'&scope='.$scope.'&response_type='.$response_type.'&redirect_uri='.$redirect;

echo "<h3>Trigger authentication -> <a href='".$auth_url."'>OAuth</a></h3>";


$CODE = $_GET['code'];

if($CODE){
   extract($_POST);
   $authorize = "authorization_code";
   $url = 'https://disqus.com/api/oauth/2.0/access_token/';
   $fields = array(
     'grant_type'=>urlencode($authorize),
     'client_id'=>urlencode($PUBLIC_KEY),
     'client_secret'=>urlencode($SECRET_KEY),
     'redirect_uri'=>urlencode($redirect),
     'code'=>urlencode($CODE)
);


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);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$data = curl_exec($ch);

curl_close($ch);

$auth_results = json_decode($data);

echo "<p><h3>The authentication information returned:</h3>";
var_dump($auth_results);
echo "</p>";

$access_token = $auth_results->access_token;

echo "<p><h3>The access token you'll use in API calls:</h3>";
echo $access_token;
echo "</p>";
echo $auth_results->access_token;

function getData($url, $SECRET_KEY, $access_token){

    //Setting OAuth parameters
    $oauth_params = (object) array(
      'access_token' => $access_token, 
      'api_secret' => $SECRET_KEY
      );

      $param_string = '';


      //Build the endpiont from the fields selected and put add it to the string.

      //foreach($params as $key=>$value) { $param_string .= $key.'='.$value.'&'; }
      foreach($oauth_params as $key=>$value) { $param_string .= $key.'='.$value.'&'; }
      $param_string = rtrim($param_string, "&");

      // setup curl to make a call to the endpoint
      $url .= $param_string;

      //echo $url;
      $session = curl_init($url);

      // indicates that we want the response back rather than just returning a "TRUE" string
      curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($session,CURLOPT_FOLLOWLOCATION,true);

      // execute GET and get the session backs
      $results = curl_exec($session);
      // close connection
      curl_close($session);
      // show the response in the browser
      return  json_decode($results);
}


//Setting the correct endpoint
$cases_endpoint = 'https://disqus.com/api/3.0/users/details.json?';

//Calling the function to getData
$user_details = getData($cases_endpoint, $SECRET_KEY, $access_token);


echo "<p><h3>Getting user details:</h3>";
var_dump($user_details);
echo "</p>";

//Setting the correct endpoint
$forums_endpoint = 'https://disqus.com/api/3.0/users/listForums.json?';

//Calling the function to getData
$forum_details = getData($forums_endpoint, $SECRET_KEY, $access_token);
echo "<p><h3>Getting forum details:</h3>";
var_dump($forum_details);
echo "</p>";
}

0 个答案:

没有答案