joshrps / laravel-shopify-API-wrapper

Interface designed for Shopify apps created with Laravel
MIT License
93 stars 48 forks source link

The requested URL returned error: 400 Bad Request #28

Closed connor11528 closed 6 years ago

connor11528 commented 7 years ago

I have a function where I would like to connect to my shopify app. I am following this tutorial

I send a POST request from the client side to authenticate my app to use shopify data. I currently have my data values hardcoded in order to connect to the API

    public function connect_shopify()
    {
        $shopify = \App::make('ShopifyAPI', [ 
            'API_KEY'       => 'eb2c00e.....a2d3', 
            'API_SECRET'    => 'f5f07f1bdf1.......0a2f5112',  // listed as Shared secret in shopify console
            'SHOP_DOMAIN'   => 'employbl.myshopify.com/', 
            'ACCESS_TOKEN'  => 'dba4169...........e0e3ab441'  // listed as password in shopify console
        ]); 

        $result = $shopify->getAccessToken();

       return response()->json(compact($result));
    }

When I run this code I get an error of 400 bad request. If I swap the values in API_SECRET and ACCESS_TOKEN I also get the bad request error.

What am I doing wrong in my request formatting and how can I connect to the shopify API?

Full error output:

[2017-10-02 22:11:25] local.ERROR: ERROR #22: The requested URL returned error: 400 Bad Request {"exception":"[object] (Exception(code: 0): ERROR #22: The requested URL returned error: 400 Bad Request at /Users/connorleech/Projects/StitchLite/vendor/rocket-code/shopify/src/RocketCode/Shopify/api.php:309)
[stacktrace]
#0 /Users/connorleech/Projects/StitchLite/vendor/rocket-code/shopify/src/RocketCode/Shopify/api.php(88): RocketCode\\Shopify\\API->call(Array, false)
#1 /Users/connorleech/Projects/StitchLite/app/Http/Controllers/ProductController.php(29): RocketCode\\Shopify\\API->getAccessToken()
danny-ng commented 6 years ago

Try excluding the ACCESS_TOKEN field from the constructor. The access token is only returned after the store has given your app permission (unless you have it stored somewhere already).

$shopify = \App::make('ShopifyAPI', [ 
            'API_KEY'       => 'eb2c00e.....a2d3', 
            'API_SECRET'    => 'f5f07f1bdf1.......0a2f5112',  // listed as Shared secret in shopify console
            'SHOP_DOMAIN'   => 'employbl.myshopify.com', // Remove trailing '/'
]);

You will also need to run $shopify->installURL() (see README for example) to obtain the authorization link to allow the user to login and grant permission to your app.

$authorization_uri = $shopify->installURL([
            'permissions' => array('write_orders', 'write_products'),
            'redirect' => 'http://myapp/success'  // update this to where Shopify should redirect the user
        ]);
echo sprintf('<a href=\'%s\'>Shopify Login</a>', $authorization_uri);

This link will direct you to login to Shopify and grant your app permission to the store, and then redirect to the URL specified in the installURL() parameter. From there, you should perform validations on hmac and then obtain the access code, which will allow you to make API requests.