Solved

Can't able to fetch session from custom API

Divy_tatva
Shopify Partner
32 1 12

Hello 

 

I have created product review form extension and in the frontstore user can add product reviews . For that i have created custom route in Web/index.js folder.

 

app.post("/api/reviews" ,  async (_req, res) => {

  try {
    const hostname = _req.hostname;
    const shopifyObj = await shopifyApi({
      // The next 4 values are typically read from environment variables for added security
      apiKey: process.env.SHOPIFY_API_KEY,
      apiSecretKey: process.env.SHOPIFY_API_SECRET,
      scopes: ['read_products','write_products'],
      hostName: hostname
    });
    const sessionId = await shopifyObj.session.getCurrentId({
      isOnline: true,
      rawRequest: _req,
      rawResponse: res,
    });

    const session = await getSessionFromStorage(sessionId);
    const client = new shopify.api.clients.Graphql( { sessionId } );
  await addReview(client,_req.body);
    res.status(200).send("success");
  } catch (e) {
    console.log(e.message);
    res.status(500).send(e.message);
  }

 });
 
 
But it's showing an Error:- [shopify-api/ERROR] Missing Authorization header, was the request made with authenticatedFetch? | {isOnline: false}
 
Also, In your documents we can fetch session id from this function =>    const session = await getSessionFromStorage(sessionId);
 
Could you please share"getSessionFromStorage" function because i am unable to find it ?
 
Thank you
Accepted Solution (1)
Divy_tatva
Shopify Partner
32 1 12

This is an accepted solution.

Hello @Van_Nguyen_GSG 

Now I am able to fetch sessions using the below method.

 

 
const session = new shopify.config.sessionStorage.findSessionsByShop(shop);
 
Thank you
 

View solution in original post

Replies 15 (15)

Van_Nguyen_GSG
Shopify Partner
148 23 40

Hi @Divy_tatva 

 

I think you misunderstood the documentation, you first need to create a custom app which authenticates using the oauth flow, this will then give you an auth token which you set as a Http header for requests using the Shopify api.

See https://shopify.dev/docs/apps/auth/oauth

Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Divy_tatva
Shopify Partner
32 1 12

Hello Kat_nguyen

 

I have already created custom app and connected with Shopify CLI generated code. In default generated code, i think oAuth flow is already set for APIs because product create API is already working which was calling from admin APP.  

 

In default code client is fetch by => 

  const client = new shopify.api.clients.Graphql({
res.locals.shopify.session
});
 
Above flow is working because of calling API from Admin app, But when tried same way for my Custom route which was calling from frontstore it's not working
Van_Nguyen_GSG
Shopify Partner
148 23 40

Okay, are you using the @Shopify/shopify-app-express package as it wasn't clear from your example?

If so, have you tried:

 

  1. setting isOnline: false
  2. ensuring that the middleware is calling app.use("/api/*", shopify.validateAuthenticatedSession()); before the app.post("/api/reviews"
Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Divy_tatva
Shopify Partner
32 1 12

Hello Kat_nguyen

 

I have made above changes. Now it's showing this errors.

1)  [shopify-api/ERROR] Missing Authorization header, was the request made with authenticatedFetch? | {isOnline: false}
2)  [shopify-app/INFO] Session was not valid. Redirecting to /api/auth?shop

Van_Nguyen_GSG
Shopify Partner
148 23 40

@Divy_tatvawhat database are you storing your sessions in? Have you checked that the token is being saved? (Note there are 2 types of tokens, offline & online, by setting isOnline to false, you querying the database for an offline token, this should be obvious from the ID column in the database)

Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Divy_tatva
Shopify Partner
32 1 12

I haven't integrate database Yet. earlier i set Isonline value is "true" but throwing same error.

Van_Nguyen_GSG
Shopify Partner
148 23 40

what does your function getSessionFromStorage(sessionId) return then? This is supposed to return a valid Session? maybe try console.log and look at whats returned in your terminal.

See: https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/session-storage.md

Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Van_Nguyen_GSG
Shopify Partner
148 23 40

Also noticed you passing in a sessionId into new shopify.api.clients.Graphql( { sessionId } ), when it should be a valid { session }, thats why you called const session = await getSessionFromStorage(sessionId) on the previous line.

see: https://github.com/Shopify/shopify-api-js/blob/main/docs/reference/clients/Graphql.md

Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Divy_tatva
Shopify Partner
32 1 12

{sessionId} was bymistake added to that client.

Divy_tatva
Shopify Partner
32 1 12

As i asked in my first question where i can find getSessionFromStorage function because it's showing an error. => "getSessionFromStorage is not defined".

 

But currently i am not able to fetch "sessionId" after that i can check what is value returned from that "getSessionFromStorage" function.

Van_Nguyen_GSG
Shopify Partner
148 23 40

I'd recommend using one of the Shopify developed Adapters https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/session-storage.md

Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Divy_tatva
Shopify Partner
32 1 12

Can you please suggests that which library give me "getSessionFromStorage" method ?

 

Because i unable to found it in my cli 3 structure and also your give document doesn't mentioned it.

Van_Nguyen_GSG
Shopify Partner
148 23 40

@Divy_tatvathat would depend on what database you using to store the session, the get session method simply retrieves the session you previously stored.

For mysql see: https://github.com/Shopify/shopify-app-js/blob/db770f741e57a031fedb194efa4c947ba2039f95/packages/sho...

All the supported databases are listed on the previous link I shared.

Helping Shopify merchants build stores that convert.
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- If you still need help, feel free to reach out to me at: van@glue.sg
Cheers!
Divy_tatva
Shopify Partner
32 1 12

This is an accepted solution.

Hello @Van_Nguyen_GSG 

Now I am able to fetch sessions using the below method.

 

 
const session = new shopify.config.sessionStorage.findSessionsByShop(shop);
 
Thank you
 
Divy_tatva
Shopify Partner
32 1 12

And Also , I am using APP proxy for call frontstore API.