Search code examples
phpreactjslaravelshopifyshopify-app

Shopify: Missing Authorization key in headers array in Laravel


I’m new to Shopify App development. I’ve developed an app using a php template provided by shopify. It’s using Laravel & React. It’s https://github.com/Shopify/shopify-app-template-php/tree/cli_three

When I execute npm run dev, the app is running in the development store.

Then, I deployed my app on the AWS server using one domain (e.g: sub.mydomain.com).

And then I installed app in the store.

The pages are rendering in the app. But I'm getting 500 error on making any GET or POST Request.

Upon debugging, I Found that this line of code $session = Utils::loadCurrentSession($request->header(), $request->cookie(), $isOnline); is throwing an exception and the error is:

Shopify\Exception\MissingArgumentException {#334
#message: "Missing Authorization key in headers array"
#code: 0
#file: "/var/www/html/shopify-project/web/vendor/shopify/shopify-api/src/Auth/OAuth.php"

But in the header of request, there is authorization code:

:authority: sub.mydomain.com
:method: GET
:path: /api/view-id
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,ne;q=0.8
authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczpcL1wvcW9kZWFuYWx5dGljcy5teXNob3BpZnkuY29tXC9hZG1pbiIsImRlc3QiOiJodHRwczpcL1wvcW9kZWFuYWx5dGljcy5teXNob3BpZnkuY29tIiwiYXVkIjoiNDBhYWM4OWM0Yzc4YmJlMjE5ZDBmMWY3NWExZjJhZTciLCJzdWIiOiI4NDM4NjcwOTc2MSIsImV4cCI6MTY1NzgxOTUyOSwibmJmIjoxNjU3ODE5NDY5LCJpYXQiOjE2NTc4MTk0NjksImp0aSI6ImRhZTI1ZjViLWIxODgtNGZkOS05MjcwLWNkOWNlODQ3MDIyZCIsInNpZCI6IjJmNTJkOTcxNTgxMDc5YmYxYmI0NDNlZWY5MGM2YTI2OGEyMmRkY2ZlZmRhMDc2YzE4NTM5OGY3YzU4ZDJmYTgifQ.ASJOsmBb4kMZz-QiRzg60pzvwDaj4w5DbZRO8mcWt1U
cache-control: no-cache
pragma: no-cache
referer: https://sub.mydomain.co/setup? hmac=633d1f9577273c7e7281dba9ca206037100dckjlkjb45ac7271674691acc6f7e6b3da&host=cW9kZWFuYWx5dGljcy5teXNob3BpZ54654nkuY29tL2FkbWlu&locale=en&session=78bbfd9e29b55c907762eba75a4a759f0691572c1f4661500f0d6d4ff6a54d86&shop=mystore.myshopify.com&timestamp=1657819465
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Linux"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
x-requested-with: XMLHttpRequest

I believe, this is due the request sent from iframe. Can anybody tell me what should be done to allow this request?


Solution

  • I got the solution! Actually, the Laravel template I am using didn't have .htaccess file inside public folder. I had to add it manually.

    On adding .htaccess file it had only this code:

    RewriteEngine on
    
    RewriteCond $1 !^(index.php|resources|robots.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    

    And the authorization code was not reaching to the Lararvel app.

    I had to add this code in .htaccess file:

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    

    And the final code in .htaccess file was:

    RewriteEngine on
    
    RewriteCond $1 !^(index.php|resources|robots.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    

    And then, all the requests started working.