title: How to get an authentication token for Google Cloud Translation API within browser (chrome extension)
body: <p>I'm trying to make a Chrome extension that uses the <a href="https://cloud.google.com/translate/docs/" rel="noreferrer">Google Cloud Translation API</a>. I followed a bunch of instructions on many different pages in the docs for Google Translate and for <a href="https://developer.chrome.com/extensions" rel="noreferrer">developing Chrome extensions</a> but I still can't figure out how to actually use the API from within a Browser.</p>
  
  <p>The farthest I've gotten was from following part of the <a href="https://cloud.google.com/translate/docs/quickstart" rel="noreferrer">Quickstart guide</a> where you create a service account, get a service account key file (downloaded as a .json file), install and initialize the Google SDK, set the <code>GOOGLE_APPLICATION_CREDENTIALS</code> environment variable to the path of the service account JSON key file, and then make a <code>curl</code> request using <code>gcloud auth application-default print-access-token</code> to get an access token to be inputted as part of the request header: "Authorization: Bearer (token here)". Successfully retrieved a translation that way.</p>
  
  <p>But within the environment of an extension you can't run commands like <code>gcloud</code> so that doesn't work. I was able to manually retrieve an access token using the <code>gcloud</code> command, and then paste that into my javascript code as part of the request header (e.g. <code>xmlHttpRequest.setRequestHeader('Authorization', 'Bearer ya29.c.Elr6BRuTfOtherRandomLetters');</code>). But that access token expires after 60 minutes so that doesn't sustainably work.</p>
  
  <p>I also tried using the <a href="https://developer.chrome.com/apps/identity#method-getAuthToken" rel="noreferrer">identity.getAuthToken</a> method (after updating things in <code>manifest.json</code> the way the documentation says to), but that didn't work. Using my service account client ID as the <code>client_id</code> under <code>oauth2</code> in <code>manifest.json</code> resulted in an "Invalid client ID" error from <code>identity.getAuthToken</code>. On the other hand, generating an OAuth2 Client ID from Google Console, and using <code>myoauth2clientid.apps.googleusercontent.com</code> as the <code>client_id</code> under <code>oauth2</code> and <code>["https://www.googleapis.com/auth/cloud-translation", "https://www.googleapis.com/auth/cloud-platform"]</code> as the <code>scopes</code> under <code>oauth2</code> resulted in an "OAuth2 not granted or revoked" error from <code>identity.getAuthToken</code>.</p>
  
  <p>Generating an API key from the Google Console page, and using that directly as the <code>key</code> parameter of my translation query, didn't even work: I received a "The request is missing a valid API key." error.</p>
  
  <p>I am really at a loss here. Any help would be awesome.</p>