-
Notifications
You must be signed in to change notification settings - Fork 48
Description
I need some help with the API calls on my first embedded application developed in Laravel. So far, I've been able to install my app and to see some basic (html) code in the backend of my shop.
However, I would like to display some products in my embedded application, and I'm having a hard time to connect with the API:
In the controller of my callback page, I'm have the following:
public function callback(Request $request){
$shopify = \App::make('ShopifyAPI');
$code = $request->input('code');
$shopify->setup(['API_KEY' => env('SHOPIFY_APP_API_KEY'), 'API_SECRET' => env('SHOPIFY_APP_API_SECRET'), 'SHOP_DOMAIN' => $request->input('shop')]);
try
{
$verify = $shopify->verifyRequest($request->input());
if ($verify)
{
$token = $shopify->getAccessToken($code);
$shopify->setup(['ACCESS_TOKEN' => $token]);
// Gets a list of products
$result = $shopify->call([
'METHOD' => 'GET',
'URL' => '/admin/products.json?page=1'
]);
$products = $result->products;
return view('shopify.callbackView', compact('products'));
}
else
{
echo 'Issue with data';
}
}
catch (Exception $e)
{
echo '<pre>Error: ' . $e->getMessage() . '</pre>';
}
}
However, I have tracked the issue to the callback view ('shopify.callbackView'), in the ShopifyApp.init method to be more specific:
<head>
<script src="https://cdn.shopify.com/s/assets/external/app.js"></script>
<script type="text/javascript">
ShopifyApp.init({
apiKey: '{{ env('SHOPIFY_APP_API_KEY') }}',
shopOrigin: 'https://{{$_GET['shop'] }}'
});
</script>
</head>
<body>
@foreach ($products as $product)
{{ $product->title }}
@endforeach
</body>
Apparently it doesn't recognize the code value on the URL (but I see it in the URL) and throws an error: ErrorException in api.php line 64: Undefined index: code
If I remove the javascript code that has the ShopifyApp on it, I get the body displayed, but not embedded in the backend of Shopify.
Not sure where the problem is and if that code is using the best practices. I would appreciate any help/guidance you can provide.
Thanks