Shopify oauth 在尝试在 PHP 中使用 curl 获取访问令牌时返回 false


Shopify oauth returns false while trying to fetch the access token using curl in PHP

我面临着同样的问题,例如:[https://stackoverflow.com/questions/32516932/shopify-oauth-returns-false-while-trying-to-fetch-the-access-token-using-curl][1]

它返回假。但我使用了正确的client_id、secrect_id和代码,

代码示例:

$post = [
  'client_id' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'client_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'code'   =>  'xxxxxxxxxxxxxxxxxxxx'
];
$ch = curl_init();
curl_setopt($ch,    
CURLOPT_URL,"https://dummy.myshopify.com/admin/oauth/access_token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

如果有人知道这个问题,请尽早给出答案。

找到解决方案。 $post应该是类似array("client_id"=>$client_id,"client_secret"=>$client_secret,"code"=>$code);

它对我有用。

$client_secret = 'SECRET_KEY';
$api_key = 'API_KEY';
$code = $_GET['code'];//return from callback url params after successfully install app.
$fields_post = "client_id=$api_key&client_secret=$client_secret&code=".$code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://{shop_url}/admin/oauth/access_token");
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_post);
curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
$xmlResponse = curl_exec ($ch);
$result = json_decode($xmlResponse);
echo $result->access_token;