In my partner account I am building an App (productmetafields) and attempting to install into test store https://productmetafields.myshopify.com/ . It almost succeeds to the install url however displays the following:
I assume that the red circle with a line through it implies there is a problem. I am unable to determine where the problem lies.
The url that contains the oauth is : https://productmetafields.myshopify.com/admin/oauth/request_grant?access_change_uuid=3e9c4e36-be79-437b-a466-bf189317a3d5&client_id=f01a9da7720fc583ff41ebdf565924c1
This is my install.php code:
<?php
// Define constants for your API Key and Shared Secret
define('SHOPIFY_API_KEY', 'api-key');
define('SHOPIFY_SHARED_SECRET', 'api-password');
// Function to generate the install URL
function generateInstallUrl($shop, $apiKey, $scopes, $redirectUri) {
$installUrl = "https://{$shop}/admin/oauth/authorize?";
$installUrl .= "client_id={$apiKey}&";
$installUrl .= "scope={$scopes}&";
$installUrl .= "redirect_uri={$redirectUri}";
return $installUrl;
}
// Function to redirect the user to the install URL
function redirectToInstall($shop, $apiKey, $scopes, $redirectUri) {
$installUrl = generateInstallUrl($shop, $apiKey, $scopes, $redirectUri);
header("Location: {$installUrl}");
exit();
}
// Ensure the 'shop' parameter is present
if (empty($_GET['shop'])) {
exit('Invalid request! A shop parameter is required.');
}
$shop = $_GET['shop'];
$scopes = 'read_products,write_products'; // Modify the scopes as needed
$redirectUri = urlencode('https://simpleshopifyapps.online/productmetafields/callback.php'); // Your OAuth callback URL
// Redirect to the Shopify install page
redirectToInstall($shop, SHOPIFY_API_KEY, $scopes, $redirectUri);
This is my callback.php
<?php
define('SHOPIFY_API_KEY', 'api-key');
define('SHOPIFY_SHARED_SECRET', 'secret-key');
function validateHmac($params, $sharedSecret) {
if (!isset($params['hmac'])) {
return false;
}
$hmac = $params['hmac'];
$params = array_diff_key($params, array('hmac' => ''));
ksort($params);
$computedHmac = hash_hmac('sha256', http_build_query($params), $sharedSecret);
return hash_equals($hmac, $computedHmac);
}
function getAccessToken($shop, $code) {
$url = "https://{$shop}/admin/oauth/access_token";
$payload = http_build_query([
'client_id' => SHOPIFY_API_KEY,
'client_secret' => SHOPIFY_SHARED_SECRET,
'code' => $code
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
function storeAccessToken($shop, $accessToken) {
// Implement your logic to store the access token
// Example: Save the token in your database
}
// Ensure the necessary parameters are present
if (empty($_GET['shop']) || empty($_GET['code']) || empty($_GET['hmac'])) {
exit('Invalid request! Missing parameters.');
}
$shop = $_GET['shop'];
$code = $_GET['code'];
$hmac = $_GET['hmac'];
// Validate the HMAC
if (!validateHmac($_GET, SHOPIFY_SHARED_SECRET)) {
exit('Invalid HMAC: Verification failed.');
}
// Request the access token
$tokenResponse = getAccessToken($shop, $code);
if (isset($tokenResponse['access_token'])) {
// Store the access token and proceed further
storeAccessToken($shop, $tokenResponse['access_token']);
// Redirect or show success message
echo 'App Installed Successfully!';
// Optionally redirect the user to your application's dashboard
// header('Location: https://yourapp.com/dashboard');
} else {
exit('Failed to get access token.');
}
Any suggestions as to why my app will not install much appreciated.
Thank you,
Edward