Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I do a Post? I cannot get the accessToken. #1272

Open
joelveloso opened this issue May 25, 2023 · 0 comments
Open

How can I do a Post? I cannot get the accessToken. #1272

joelveloso opened this issue May 25, 2023 · 0 comments

Comments

@joelveloso
Copy link

Issue summary

ORDERS_UPDATED: { deliveryMethod: DeliveryMethod.Http, callbackUrl: "/api/webhooks", callback: async (topic, shop, body, webhookId, apiVersion) => { console.log("Received ORDERS_UPDATED webhook"); try { let session = await shopify.sessionStorage.loadSession(shop); if (!session) { console.error(Failed to load session for shop: ${shop}`);
return;
}
} catch (error) {
console.error("Error loading session:", error);
}

  let accessToken = session.accessToken;

  const payload = JSON.parse(body);

  if (payload.closed_at === null) {
    console.log("Order is not closed. Triggering flow...");
    const url = `https://${shop}/admin/api/${apiVersion}/graphql.json`;
    // console.log(payload);
    const triggerFlow = async (id) => {
      console.log("Inside triggerFlow function. Order ID:", id);

      const query = `
        mutation {
          flowTriggerReceive(
            body: "{
              \\"trigger_id\\": \\"##########################\\",
              \\"properties\\": {
                \\"order_id\\": ${id}
              }
            }"
          ) {
            userErrors {field, message}
          }
        }
      `;

      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/graphql",
          "X-Shopify-Access-Token": accessToken,
        },
        body: JSON.stringify({ query }),
      });

      const responseData = await response.json();
      console.log(responseData);
    };

    // Trigger the Shopify Flow
    triggerFlow(payload.id)
      .then(() => {
        console.log("Flow triggered successfully");
      })
      .catch((error) => {
        console.error("Error triggering flow:", error);
      });
  } else {
    console.log("Order is closed. Not triggering flow.");
  }
},

},
};
`

This part:

let session = await shopify.sessionStorage.loadSession(shop);

Does not work. I am using your template. From the readme.MD it reads:

The Node app template comes with the following out-of-the-box functionality:

OAuth: Installing the app and granting permissions

How can I get the, or if someone can point me in the right direction

"X-Shopify-Access-Token": accessToken,

Kind Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant