Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Cannot make Admin API post request from proxy route #56

Closed
lmartins opened this issue Jul 20, 2018 · 10 comments
Closed

Cannot make Admin API post request from proxy route #56

lmartins opened this issue Jul 20, 2018 · 10 comments
Assignees
Labels
bug Bug with the code feature Enhancement to the code

Comments

@lmartins
Copy link

I'm building an app that exposes a frontend on the public that feeds from proxy routes to pull data from the store.

This is a product builder, which at the end of the process should make a proxy request, which in turn has a controller method to create a new product in Shopify using the REST API routes.

I've tried two approaches, both resulting in different server errors.

Using the examples from the documentation, I've tried to instantiate a ShopifyApp object as follows:

    $shop = ShopifyApp::shop();

    $response = $shop->api()->request('post', '/admin/products.json', [
      'product' => [
        'title' => 'teste'
      ]
    ]);
    $products = $response->body;

This results in the following error message:

FatalThrowableError: Call to a member function api() on null

With null referring to the $shop instance.

So I've tried a different approach, where I would instantiate from the BasicShopifyAPI class instead, passing the necessary credentials:

    $api = new BasicShopifyAPI(true);
    $api->setShop(env('SHOPIFY_SHOP_URL'));
    $api->setApiKey(env('SHOPIFY_API_KEY'));
    $api->setApiPassword(env('SHOPIFY_API_SECRET'));

    $response = $api->request('post', '/admin/products.json', [
      'product' => [
        'title' => 'test2'
      ]
    ]);
    $products = $response->body;

In this case I get a different error message:

GuzzleHttp\Exception\ClientException: Client error: `POST https://f155dfa1e833a841e33573e00fe3272d:***@storename.myshopify.com/admin/products.json` resulted in a `401 Unauthorized` response:
{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

I've double checked the env credential variables and they are accurate.

My guess this is because the proxy routes do not use the auth.shop middleware, so I wonder what could be done to get around this and make this request succeed.

Thanks so much for any help.

@lmartins
Copy link
Author

Please excuse me as this perhaps doesn't relate directly to this library. I guess I'm just trying to understand if the library is supposed to allow this use case.

I did managed to make a post request to /admin/products.json Admin API endpoint by using an instance of GuzzleHttp\Client directly, attaching the access token to the request.

As apparently there were others with similar questions:
#48
#34

I wonder if this is something you could shed some light on it.

Thank you!

@gnikyt
Copy link
Owner

gnikyt commented Jul 24, 2018

Ah from a Proxy route?

$shop = ShopifyApp::shop(); depends on picking up on the shop session. Which is set from the auth middleware.

This doesn't get set for proxy... I think I'll mark this as a bug as there no support/code for setting the shop session from proxy which would solve the issue of why you get the null error.

@gnikyt gnikyt self-assigned this Jul 24, 2018
@gnikyt gnikyt added feature Enhancement to the code bug Bug with the code labels Jul 24, 2018
@lmartins
Copy link
Author

@ohmybrew yeah, sorry, I should have made that clearer. This is from indeed from a proxy route.
Thanks so much for looking into this Tyler.

@gnikyt
Copy link
Owner

gnikyt commented Jul 24, 2018

Commit 2b1a131 should solve this (not yet merged). Which version are you on currently @lmartins ?

@lmartins
Copy link
Author

@ohmybrew I'm currently running 2.3

@gnikyt
Copy link
Owner

gnikyt commented Jul 24, 2018 via email

@lmartins
Copy link
Author

Oh, I can upgrade this to the latest if it helps

@gnikyt
Copy link
Owner

gnikyt commented Jul 24, 2018 via email

@gnikyt
Copy link
Owner

gnikyt commented Jul 24, 2018

This should be solved now. Tagged v3.0.1 for currents.

You should be able to tell composer to pull v2.3.2 to grab the fix.

@lmartins
Copy link
Author

Hi @ohmybrew sorry I got carried away with my work and forgot to confirm this. It does solve it! :)

Thanks so much for your help and this awesome library Tyler!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug with the code feature Enhancement to the code
Projects
None yet
Development

No branches or pull requests

2 participants