I am new to shopify app development and I am using the API. While doing the OAuth for the app I am stuck at the step where we have to retrieve the permanent access token. I have the access code from the first step and afterwards I am POSTing the the three parameters using cURL.
This is the code that I have written:
<?php $code=$_GET['code'];
$client_id="c0356e832edeb92d5e30bce50ddb039f";
$client_secret="xxx";
$url="https://smokeallover-2.myshopify.com/admin/oauth/access_token";
$fields_string="";
$fields = array('client_id' => urlencode($client_id),
'client_secret' => urlencode($client_secret),
'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_HTTPHEADER, array('Content-Type:application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
if($result === false)
echo curl_error($ch);
curl_close($ch);
echo $result;
?>
but the result is error:
invalid request.
I have found many questions similar to this on stackoverflow and have tried reinstalling the app among other things.. I am not sure where to find the error as I am new to cURL, OAuth2 and shopify API.. I have figured out until this point. Please help me find a lead.