Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3637

Authenticate to Google Language API using API Key for REST based web app

$
0
0

I am receiving an error 401 when trying to authenticate to Google API using an API Key.

The following is the javascript code used to make the call:

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);            });    }

Same results happen when using postman:URL: https://translation.googleapis.com/language/translate/v2Method: POST

Query Params:q=the house is built with woodtarget=fr-CA

Headersx-goog-user-project: projectnameAuthorization: Bearer APIKEYINSERTEDHEREContent-Type: application/json; charset=utf-8

Response{"error": {"code": 401,"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.","errors": [{"message": "Invalid Credentials","domain": "global","reason": "authError","location": "Authorization","locationType": "header"}],"status": "UNAUTHENTICATED","details": [{"@type": "type.googleapis.com/google.rpc.ErrorInfo","reason": "ACCESS_TOKEN_TYPE_UNSUPPORTED","metadata": {"method": "google.cloud.translate.v2.TranslateService.TranslateText","service": "translate.googleapis.com"}}]}}

Appreciate any insights anyone has why I can't make a basic request using the API key for authentication.

Various authentication methods.Read the documentation. Debugged using POSTMANThe error messages keep changing.


Viewing all articles
Browse latest Browse all 3637

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>