Notice: We've implemented a new Partners and Developers board structure and moved content into the new structure. To find your previously submitted topics and replies click on your avatar in the top right followed by My Profile.

How to fetch request to obtain access token for the Customer Account API?

How to fetch request to obtain access token for the Customer Account API?

Willd99
Shopify Partner
6 0 0

Hi there, I'm trying to use Customer Account API to enable login experience for my shop. For more context, my app is Confidential type.

 

Following the docs for the api, I'm using this code:

 

const clientId = process.env.CLIENT_ID;
const body = new URLSearchParams();

body.append('grant_type', 'authorization_code');
body.append('client_id', clientId);
body.append(
  'redirect_uri',
  `<redirect_uri>`,
);
body.append('code', code);

// Public Client
const codeVerifier = localStorage.getItem('code-verifier');
body.append('code_verifier', codeVerifier);

const headers = {
  'content-type': 'application/x-www-form-urlencoded',
  // Confidential Client
  'Authorization': 'Basic `<credentials>`'
}

const response = await fetch(`https://shopify.com/<shop_id>/auth/oauth/token`, {
  method: 'POST',
  headers: headers,
  body,
});

interface AccessTokenResponse {
  access_token: string;
  expires_in: number;
  id_token: string;
  refresh_token: string;
}

const {access_token, expires_in, id_token, refresh_token} =
  await response.json<AccessTokenResponse>();

and I'm using this code to generate the auth inside headers (this is also taken from the docs)

export async function generateState(): Promise<string> {
  const timestamp = Date.now().toString();
  const randomString = Math.random().toString(36).substring(2);
  return timestamp + randomString;
}

So my question here is, do I need to convert the result of that function into base64-encoded string? Because I kept getting "invalid client" error (The client identifier provided is invalid, the client failed to authenticate, the client did not include its credentials, provided multiple client credentials, or used unsupported credentials type.)

Replies 2 (2)

harminder
Shopify Partner
1 0 1

Anyone able to solve this issue, I keep getting 

"error": "invalid_client",
"error_description": "The client identifier provided is invalid, the client failed to authenticate, the client did not include its credentials, provided multiple client credentials, or used unsupported credentials type."
nolwag
Shopify Partner
4 0 1

Same..