I’m working on creating a Shopify app and trying to get an access token through OAuth. I keep getting an error that says the request isn’t from Shopify.
My authorization file (auth.php):
<?php
// Configuration settings
$store_name = "my-test-shop";
$client_id = "abc123def456ghi789jkl";
$permissions = "read_products,write_orders";
$callback_url = "http://localhost/myapp/token_handler.php";
// Create authorization URL
$auth_url = "https://" . $store_name . ".myshopify.com/admin/oauth/authorize?client_id=" . $client_id . "&scope=" . $permissions . "&redirect_uri=" . urlencode($callback_url);
// Send user to Shopify
header("Location: " . $auth_url);
exit();
After clicking install, it sends me back to my callback URL with parameters like code, hmac, shop, etc. But my token handler keeps rejecting it.
My token handler (token_handler.php):
<?php
// App configuration
$store_name = "my-test-shop";
$client_id = "abc123def456ghi789jkl";
$client_secret = "xyz789abc123def456";
$auth_code = $_GET["code"];
$request_timestamp = $_GET["timestamp"];
$request_signature = $_GET["signature"];
// Build signature string for verification
$signature_string = $client_secret . "code=" . $auth_code . "shop=" . $store_name . ".myshopify.comtimestamp=" . $request_timestamp;
// Verify the request came from Shopify
if (md5($signature_string) === $request_signature) {
// Prepare token request
$token_params = array(
"Content-type" => "application/json",
"client_id" => $client_id,
"client_secret" => $client_secret,
"code" => $auth_code
);
// Make API call to get token
$api_response = make_shopify_request(NULL, $store_name, "/admin/oauth/access_token", $token_params, 'POST');
// Parse the response
$response_data = json_decode($api_response['response'], TRUE);
$access_token = $response_data['access_token'];
echo $access_token;
} else {
die('This request is NOT from Shopify!');
}
I’ve tried hosting it on different servers including HTTPS ones but the signature verification always fails. The MD5 hash I generate never matches what Shopify sends. What could be wrong with my signature verification logic?