Код: Выделить всё
error:
code: 401
errors: [{…}]
message: "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity-sign-in/web/devconsole-project."
status: "UNAUTHENTICATED"
Код: Выделить всё
$(document).ready(async function() {
if ("{{.DriveCookie}}" === "") {
iziToast.info({
title: 'Warning!',
message: 'Drive connection not established. Connecting...',
position: 'topCenter'
});
const accessToken = await getAccessToken();
// Post the access token to store it in Redis
const formData = new FormData();
formData.append("driveCookie", accessToken);
formData.append("slug", "[USER_SLUG]"); // Censored
fetch("/user/driveCookie", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
iziToast.success({
title: 'Success!',
message: 'Drive connection established.',
position: 'topCenter'
});
})
.catch(error => {
console.error("Error:", error);
alert("An error occurred!");
});
}
});
async function loadFile() {
const form = document.getElementById('uploadDocumentForm');
const formData = new FormData(form);
const fileInput = document.getElementById('documentFile');
if (!fileInput.files.length) {
iziToast.error({
title: 'Error',
message: 'Please select a file.',
position: 'topCenter'
});
return;
}
const file = fileInput.files[0];
let fileType = file.name.split('.').pop();
if (fileType.includes('-')) {
fileType = fileType.split('-')[0];
}
if(fileType === 'docx'){
const accessToken = await getAccessToken();
const accessTokenString = accessToken.toString();
const metadata = {
name: file.name,
mimeType: file.type,
parents: ['[PARENT_FOLDER_ID]'] // Censored
};
const formData2 = new FormData();
formData2.append("metadata", new Blob([JSON.stringify(metadata)], { type: "application/json" }));
formData2.append("file", file);
try {
const response = await fetch("https://www.googleapis.com/upload/drive ... =multipart", {
method: "POST",
headers: new Headers({ "Authorization": "Bearer " + accessTokenString }),
body: formData2
});
const result = await response.json();
if (response.ok) {
formData.append('driveID', result.id);
} else {
console.error('Upload error:', result);
iziToast.error({
title: 'Error',
message: 'Error uploading file.',
position: 'topCenter'
});
}
} catch (error) {
console.error('Error:', error);
iziToast.error({
title: 'Error',
message: 'Error uploading file.',
position: 'topCenter'
});
return;
}
}
}
< /code>
async function getAccessToken() {
return new Promise((resolve, reject) => {
// Simplified for demonstration; returns the cached token
resolve(cachedAccessToken);
});
}
< /code>
To check whether the access token is stored correctly in Redis, I printed both the previously logged-in access token stored in Redis and the newly obtained access token from the latest login to the console :
Previously logged-in access token stored in Redis: ya29.a0AXeO80S-***************ME8yaCgYKAfcSARASFQHGX2MiEqNw2FBDguC2VN4xZdFq0Q0175 // Censored
< /code>
Access token obtained from a new login: ya29.a0AXeO80S-***************ME8yaCgYKAfcSARASFQHGX2MiEqNw2FBDguC2VN4xZdFq0Q0175 // Censored
< /code>
Summary:
When I perform a Google login on each page, the access token is valid and my API calls work.
I store the access token in Redis (using the above token storage code) to avoid repeated logins.
However, when I use the Redis-stored token for a Drive API call (as shown in loadFile()), I receive a 401 error, even though the token from Redis is identical to the one obtained during login.
Any insights into why the API request fails with the stored token—even when both tokens match—and how to resolve this issue would be greatly appreciated.
I stored the access token in Redis to avoid re-login across pages. I expected the stored token to work for Drive API requests, but instead, I received a 401 UNAUTHENTICATED error, even though the token matched the one from login.
Подробнее здесь: https://stackoverflow.com/questions/794 ... ated-error
Мобильная версия