Authentifizieren Sie sich mit dem API -Schlüssel für die REST -basierte Web -App mit der Google Language -API authentifiJavaScript

Javascript-Forum
Anonymous
 Authentifizieren Sie sich mit dem API -Schlüssel für die REST -basierte Web -App mit der Google Language -API authentifi

Post by Anonymous »

Ich empfange einen Fehler 401, wenn ich versuche, mit einem API -Taste mit Google API zu authentifizieren.

Code: Select all

function test()
{
const outputElement = document.getElementById('output');

const apiUrl = 'https://translation.googleapis.com/language/translate/v2';

const requestOptions = {
method: 'POST',
headers: {
'Authorization': 'Bearer APIKEYINSERTEDHERE',
'x-goog-user-project': 'projectname',
'Content-Type': 'application/json; charset=utf-8'
},
body: {
'q': 'the house is built with wood',
'target': 'fr-CA'
},
};

fetch(apiUrl, requestOptions)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
outputElement.textContent = JSON.stringify(data, null, 2);
})
.catch(error => {
console.error('Error:', error);
});
}
Die gleichen Ergebnisse treten bei der Verwendung von Postman:
URL: https://translation.googleapis.com/Guan ... anslate/v2
Methode: Post
Abfragelparamien:
q = Das Haus wird mit Holz
-Ziels gebaut. /> X-Goog-Userprojekt: ProjectName
Autorisierung: Bearer apikeyInsersedhere
Content-Type: Application /JSON; charset = utf-8 < /p>
Antwort < /strong>
{
"Fehler": {
"Code": 401,
"meldung": "Anfrage hatte ungültige Authentifizierungsanmeldeinformationen. https://developers.google.com/ididentit ... le-project.",
"fehler": [
{
meldung ":" Ungültige Anmeldeinformationen ",
argument": "Autherror",
" Lace ":" AUTHER, "," Autherror ",
"locationType": "header"
}
],
"Status": "Unauthenticated",
"Details": [
{
"@type": "type.googleapis.com/google.rpc.Rorinfo".br />" argument ". /> "metadata": {
"Methode": "google.cloud.translate.v2.translateService.translatetext",
"service": "translate.googleapis.com"
}
}
]
}
} < /p> < /p> < />
} < /> < /P. Grundlegende Anforderung unter Verwendung des API -Schlüssels zur Authentifizierung. < /p>
Verschiedene Authentifizierungsmethoden.
Lesen Sie die Dokumentation. Debugugged mit Postman
Die Fehlermeldungen ändern sich ständig. < /P>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post