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
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
1 Like
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
Okay, are you using the @Shopify_77 /shopify-app-express package as it wasn’t clear from your example?
If so, have you tried:
- setting isOnline: false
- ensuring that the middleware is calling app.use(“/api/*”, shopify.validateAuthenticatedSession()); before the app.post(“/api/reviews”
And Also , I am using APP proxy for call frontstore API.
Hello Kat_nguyen
I have made above changes. Now it’s showing this errors.
- [shopify-api/ERROR] Missing Authorization header, was the request made with authenticatedFetch? | {isOnline: false}
- [shopify-app/INFO] Session was not valid. Redirecting to /api/auth?shop
@Divy_tatva what 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)
I haven’t integrate database Yet. earlier i set Isonline value is “true” but throwing same error.
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
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
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.
{sessionId} was bymistake added to that client.
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.
1 Like
@Divy_tatva that 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/shopify-app-session-storage-mysql/src/mysql.ts#L89
All the supported databases are listed on the previous link I shared.
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