Attempt to install my App fails. OAuth?

ScinteraShopy
Shopify Partner
40 4 2

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:

Unable to Install App productmetafields.jpg

 

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-4...

 

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

Replies 5 (5)

SomeUsernameHe
Shopify Partner
475 53 95

Have you thought about using Remix and using the Shopify CLI?

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
ScinteraShopy
Shopify Partner
40 4 2

Having to learn another language might be a bit of a challenge for me. PHP is pretty much all I know. But I appreciate your suggestions. Thank you.

SomeUsernameHe
Shopify Partner
475 53 95

I would think about it, I was the same way. I only knew PHP. I didn't know React or Remix 1 month ago... but it's just JavaScript and AI can help. Let me see if I can get something setup with PHP to test your code. 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
ScinteraShopy
Shopify Partner
40 4 2

Only if you have time. I appreciate that.

ScinteraShopy
Shopify Partner
40 4 2

OMG! I fixed it. How stupid of me. All I had to do was Clear my browser cache! And now it works. Thank you.