Hi,
I have an embedded Shopify app, and now I would like to get an access token for use in an integration service running in the background.
I should be able to get that sending a post request to
https://{shop}.myshopify.com/admin/oauth/access_token
passing a session token as it says in the documentation: https://shopify.dev/docs/apps/build/authentication-authorization/access-tokens/token-exchange
To get the session token i follow this: https://shopify.dev/docs/apps/build/authentication-authorization/session-tokens/set-up-session-tokens
import createApp from "@shopify/app-bridge";
import { getSessionToken } from "@shopify/app-bridge/utilities";
const app = createApp({
apiKey: "12345", // API key from the Partner Dashboard
host: "1321321321321321", // host from URL search parameter
});
const sessionToken = await getSessionToken(app);
To get the host I do this:
// Extract 'host' parameter from the query string
const urlParams = new URLSearchParams(window.location.search);
const host = urlParams.get('host');
This is the final code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Shopify App 0.01</title>
</head>
<body>
<div>
<h1>My Shopify App 0.01</h1>
</div>
<script>
import createApp from "@shopify/app-bridge";
import { getSessionToken } from "@shopify/app-bridge/utilities";
// Extract 'host' parameter from the query string
const urlParams = new URLSearchParams(window.location.search);
const host = urlParams.get('host');
if (!host) {
console.error("Host parameter is missing!");
} else {
console.log("Host parameter:", host);
}
const app = createApp({
apiKey: "12345", // API key from the Partner Dashboard
host: host, // host from URL search parameter
});
console.log("Shopify App Bridge initialized:", app);
// Retrieve session token
const sessionToken = await getSessionToken(app);
console.log("Session Token: ", sessionToken);
</script>
</body>
</html>
I doesn’t work.
I get at a lot of errrors :
The Components object is deprecated. It will soon be removed. shopify_app.php
Firefox can’t establish a connection to the server at wss://argus.shopifycloud.com/graphql?bucket_id=gid://shopify/Shop/5456465. render-common-DBcHPi1Mdzsq.js:77:16699
The connection to wss://argus.shopifycloud.com/graphql?bucket_id=gid://shopify/Shop/5456465 was interrupted while the page was loading. render-common-DBcHPi1Mdzsq.js:77:16699
Firefox can’t establish a connection to the server at wss://argus.shopifycloud.com/graphql?bucket_id=gid://shopify/Shop/5456465. render-common-DBcHPi1Mdzsq.js:77:16699
The connection to wss://argus.shopifycloud.com/graphql?bucket_id=gid://shopify/Shop/5456465 was interrupted while the page was loading. render-common-DBcHPi1Mdzsq.js:77:16699
Uncaught SyntaxError: import declarations may only appear at top level of a module shopify_app.php:14:9
The Components object is deprecated. It will soon be removed.
I do get the host from url parameters, if I don’t create the app.
Any help is very appreciated.
BR Kresten