Access token is expired or invalid

Hello everybody,
I’m having trouble using the API with my React Web App. Steps i tried so far:

  1. Generate a new token with “https://api.baserow.io/api/user/token-auth/”:
    I get the token just fine, but when i try to use it in a request i get the message.

  2. Refresh the token i got in the previous step, using “https://api.baserow.io/api/user/token-refresh/
    Same result, i get the token, but can’t make any requests.

  3. I used a trycatch in my app when i try the token from step one, if it fails, i refresh it as in step 2.
    Same result.

It looks like my tokens are expiring too fast, maybe i’m doing it wrong?
Can anyone help?

Hi,

You receive 3 tokens after step 1: token, access_token and refresh_token. Are you sue the access_token to sign the request? It should normally be valid for 10 minutes.

yes, i tried using both the token and the access_token.
Same error.

Its weird because if i use the token manually i can request normally.
But when i try to use the same token for different requests i get the error:

That’s the function i’m using, straight from Postman (only did some minor tweaks)

const myHeaders = new Headers();
	myHeaders.append("Authorization", `JWT ${token}`);

	return fetch(query, {
		method: "GET",
		headers: myHeaders,
		redirect: "follow",
	})
		.then((response) => response.text())
		.then((result) => JSON.parse(result))
		.catch((error) => console.error(error));
}

if i use the query as “https://api.baserow.io/api/database/tables/database/103959/” (103959 is my database id)
i get the tables just fine:

 ✓ Compiled /favicon.ico in 145ms (291 modules)
[
  { id: 281207, name: 'Customers', order: 1, database_id: 103959 },
  { id: 281208, name: 'Projects', order: 2, database_id: 103959 },
  { id: 286870, name: 'Campaings', order: 3, database_id: 103959 },
  { id: 286873, name: 'Sections', order: 4, database_id: 103959 }
]

then, i use the same token and same function to do another request
now using the query as “https://api.baserow.io/api/database/rows/table/${params.id}/” (params.id is my table id)
then i get the error:

{
  detail: 'Access token is expired or invalid.',
  error: 'ERROR_INVALID_ACCESS_TOKEN'
}

If i go to Postman and refresh the token, then use the new token manually, i can do both requests normally:
image

The only thing i changed between the two is the token, and since in the first test i could request the first after trying the second, i dont think the token expired

I really don’t know what else it could be.

Hi @linikerdg, you should use the token received (token or access_token - they are the same) from token_auth endpoint to use it in the API.

The refresh token endpoint is used to get a new JWT token. You will only need to do it once your original JWT token expires. To obtain a new token, you pass in the refresh_token into the refresh token endpoint.

If you still have trouble with this please provide whole executable code with exact steps taken.

I’m not doing mutch atm, just testing.
So here it is:
Function to get token:

async function getToken() {
	const myHeaders = new Headers();
	myHeaders.append("Content-Type", "application/json");

	const raw = JSON.stringify({
		email: ***,
		password: ***,
	});
	return fetch("https://api.baserow.io/api/user/token-auth/", {
		method: "POST",
		headers: myHeaders,
		body: raw,
		redirect: "follow",
	})
		.then((response) => response.text())
		.then((result) => JSON.parse(result))
		.catch((error) => console.error(error));
}

then, in my main, i’m doing:

const token = (await getToken()).token;
	const data = await fetchData(
		"https://api.baserow.io/api/database/tables/database/103959/",
		token
	);
	console.log(data);
	return (
		<>
			<h2>Tables</h2>
			<ul>
				{data?.map((i: Table) => (
					<li key={i.id}>{i.name}</li>
				))}
			</ul>
		</>
	);

the “console.log(data)” returns:
image

You didn’t post how your fetchData function looks like.