Invalid parameter: code

1,392 views
Skip to first unread message

Pratik Thakkar

unread,
Oct 9, 2013, 11:44:30 PM10/9/13
to disqu...@googlegroups.com

Hi,

I am trying to write a script that will import all content including author email details into our database. But I keep getting the below mentioned error

stdClass Object
(
    [error_description] => Invalid parameter: code
    [error] => invalid_grant
)

If I remove the "code" parameter, I get an error saying 

The authentication information returned:

stdClass Object ( [error_description] => Missing required parameter: code [error] => invalid_request )

I would appreciate any help on this.

See my code below

<?
$PUBLIC_KEY = "XXXXXX";
$SECRET_KEY = "XXXXXX";
$redirect = "http://[domain]/disqus-import.php";

$ACCESS_TOKEN= 'XXXXXX';

$fields = array(
  'access_token'=>urlencode($ACCESS_TOKEN),
  'code'=>urlencode($ACCESS_TOKEN),
  'grant_type'=>'authorization_code',
  'client_id'=>urlencode($PUBLIC_KEY),
  'client_secret'=>urlencode($SECRET_KEY),
  'redirect_uri'=>urlencode($redirect)
);

//url-ify the data for the POST
foreach($fields as $key=>$value)
$fields_string .= $key.'='.$value.'&';

rtrim($fields_string, "&");

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
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);

//execute post
$data = curl_exec($ch);

//close connection
curl_close($ch);

//turn the string into a object
$auth_results = json_decode($data);
?>

Burak Yiğit Kaya

unread,
Oct 13, 2013, 6:49:04 PM10/13/13
to disqu...@googlegroups.com
Hey Pratik,

Code is only necessary for the authentication section to obtain a "permanent" access token. I didn't go deep in your code but it looks like you're trying to send "code" with both requests(authentication/access token obtaining AND the actual API request).


--
You received this message because you are subscribed to the Google Groups "Disqus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to disqus-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
BYK

Pratik Thakkar

unread,
Oct 23, 2013, 11:12:31 PM10/23/13
to disqu...@googlegroups.com
Hi BYK,

Thanks for your response.

I understand that code is only necessary for permanent access token. That is the objective. I am trying to develop a script that will run periodically to pull in comments from Disqus forum to our database.

And I seem to be stuck with this problem

Regards,
Praitk

Burak Yiğit Kaya

unread,
Oct 24, 2013, 12:21:41 AM10/24/13
to disqu...@googlegroups.com
I understand and again, your solution is quite clear: do not send the code parameter unless you are requesting a permanent access token and you're good.

Pratik Thakkar

unread,
Oct 24, 2013, 2:42:56 AM10/24/13
to disqu...@googlegroups.com
Thanks for your response.

I have tried that and tried again. If I retain the code parameter, then I receive the following error.

stdClass Object
(
    [code] => 2
    [response] => You must either provide a user or authenticate the user.
)

Thanks

Burak Yiğit Kaya

unread,
Oct 24, 2013, 5:22:21 AM10/24/13
to disqu...@googlegroups.com
Yes, because you need to provide an `access_token` parameter which is what you get from your initial authorize/access_token request.

Pratik Thakkar

unread,
Oct 24, 2013, 5:58:32 AM10/24/13
to disqu...@googlegroups.com
That is what I have done but doesn't help. I have added the access token provided on the Disqus Applicaton platform as "Your Access Token".

Also one thing I noticed on the Applicaton details page.

Use this token as the value for access_token to authenticate as xxxxx.

Do I need to add a user name as "xxxxx"? My current list of parameters are

'access_token'=>urlencode($ACCESS_TOKEN),
'code'=>urlencode($ACCESS_TOKEN),
'grant_type'=>'authorization_code',
'client_id'=>urlencode($PUBLIC_KEY),
'client_secret'=>urlencode($SECRET_KEY),
'redirect_uri'=>urlencode($redirect)
Am I missing something herer?

Burak Yiğit Kaya

unread,
Oct 24, 2013, 6:26:52 AM10/24/13
to disqu...@googlegroups.com
Your request looks like the request for getting the access token. Please read the API docs for a comprehensive explanation about the authentication system: http://disqus.com/api/docs/auth/

If you manage to get your access_token successfully, the headline you are looking for in that document is "Making Requests with Tokens" to make regular API requests using the token you have obtained.
Reply all
Reply to author
Forward
0 new messages