Zoho is not returning a refresh token when hitting ${accountsServer}/oauth/v2/token

19 Views Asked by At

I'm referring to this document to create access token and refresh token. https://www.zoho.com/crm/developer/docs/api/v5/access-refresh.html. But when I send this request, I get all expected properties listed in document except refresh_token.

I tried adding this to the formData but still getting the same result.

formData.append('prompt', 'consent');

Also tried changing the URL like ${accountsServer}/oauth/v2/token?prompt=consent , but getting 404 for this.

export async function AccessToken(accountsServer:string, code:string) {
  const clientId = process.env.ZOHO_CLIENTID;
  const clientSecret = process.env.ZOHO_CLIENT_SECRET;
  const redirectUri = process.env.ZOHO_REDIRECTURL;

  const reqUrl = `${accountsServer}/oauth/v2/token`;
  const formData = new FormData();

  console.log("code", code)

  formData.append('grant_type', 'authorization_code');
  formData.append('client_id', clientId);
  formData.append('client_secret', clientSecret);
  formData.append('redirect_uri', redirectUri);
  formData.append('code', code);

  try {
    const response = await fetch(reqUrl, {
      method: 'POST',
      body: formData 
    });

  

    if (!response.ok) {
      throw new Error(`HTTP error: ${response.status}`);
    }

    return await response.json();

    // const data = await response.json();
    // console.log("Access Token Response: %o", data);
    // return data;
  } catch (err) {
    console.error("Error obtaining access token: %o", err);
  }
}

0

There are 0 best solutions below