Issue with CORS during authorization Host: /v3/connect/token

When I get the code in my link and send the request Host: /v3/connect/token, I get an error CORS. How do I solve it, since the transfer data is all correct, what exactly is the problem.

Please help me to solve this problem.

{
“error”: “invalid_authentication”,
“error_code”: 25022,
“error_description”: “Invalid credentials provided”,
“error_uri”: “Nylas Unified Auth Service API Reference”,
“request_id”: “1725875101917-a437c334-9069-4c23-b4c9-b86d6884e1bb”
}

Hello @Kateryna Which SDK are you using? Can you share your source code?

Invalid credentials provided means that one the parameters is not right…looking at your source code will help us…

Also, have you registered your callbackURI as instructed here? Email API Quickstart: read and send email messages | Nylas Docs

@Blag Hello)
I use the Node.js SDK
The first step

const handleClick = () => {

  const redirectUri = 'http://localhost:8080/choosecalendar';
  const responseType = 'code';
  const accessType = 'online';
  const provider = 'google';
  const state = 'sQ6vFQN';

  const authUrl = `https://api.eu.nylas.com/v3/connect/auth?client_id=${clientIdNylas}&redirect_uri=${redirectUri}&response_type=${responseType}&access_type=${accessType}&provider=${provider}&state=${state}`;

  window.location.replace(authUrl);
};

then I get Url with quote code, state and the second step is

const exchangeCodeForToken = async (code: string) => {
  try {
      const response = await fetch('https://api.eu.nylas.com/v3/connect/token', {
          method: 'POST',
          headers: {
              'Content-Type': 'application/json',
              'Authorization': `Basic ${NylasConfig.apiKey}`
          },
          body: JSON.stringify({
              client_id: `${clientIdNylas}`,
              client_secret: `${NylasConfig.apiKey}`,
              grant_type: 'authorization_code',
              code: code,
              redirect_uri: 'http://localhost:8080/choosecalendar',
              code_verifier: 'nylas'
          })
      });

      if (!response.ok) {
          throw new Error('Network response was not ok');
      }

      const data = await response.json();
      console.log(data);
      return data;
  } catch (error) {
      console.error('Error exchanging code for token:', error);
  }
};

useEffect(() => {
  const params = new URLSearchParams(location.search);
  const code = params.get('code');

  if (code) {
    exchangeCodeForToken(code);
  }
}, [location]);

but I get error

{
    "error": "origin_not_allowed",
    "error_code": 45011,
    "error_description": "Error origin not allowed for redirect_uri for platform:js",
    "error_uri": "https://accounts.nylas.io/#tag/Event-Codes",
    "request_id": "1725887294670-367cc281-066b-43fe-981c-0e517a43e2e4"
}

Can you help me and fix this issue.

@Kateryna Check this post :slight_smile: How to generate the OAuth URL - #9 by hamzahyounus

@Blag thanks, It helps me and fix this issue

2 Likes

Awesome! Kudos to @hamzahyounus for sharing his fix :wink: