title: FCM access token key via JavaScript
body: <p>Is there any option to get the FCM access token key via Java Script on the client side?
  I'm using Firebase for a simple android app' and I want to let some users the option to send a push notification messages.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>function getAccessToken(){  }
  
  var xhr = new XMLHttpRequest();
  xhr.responseType = "json";
  xhr.open('POST', "https://fcm.googleapis.com/v1/projects/**********/messages:send", true);
  xhr.setRequestHeader('Authorization', "Bearer " + getAccessToken());</code></pre>
  </div>
  </div>
  </p>
  
  <p>I tried the suggestion of @Ali with the following code snippet but now I'm getting an exception from refreshAccessToken(...) with the message of "Scopes not configured for service account.".
  I want to clarify that the DotNet code from the first comment of Ali's answer is still working with the same JSON file.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>@JavascriptInterface
  private static String readToken() throws IOException, InterruptedException {
      String str = new String("{\n" +
              "  \"type\": \"service_account\",\n" +
              "  \"project_id\": \"*****\",\n" +
              "  \"private_key_id\": \"*****\",\n" +
              "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\n*****\\n-----END PRIVATE KEY-----\\n\",\n" +
              "  \"client_email\": \"*****\",\n" +
              "  \"client_id\": \"*****\",\n" +
              "  \"auth_uri\": \"*****",\n" +
              "  \"token_uri\": \"*****",\n" +
              "  \"auth_provider_x509_cert_url\": \"*****",\n" +
              "  \"client_x509_cert_url\": \"*****\"\n" + "}\n");
  
      InputStream stream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
      GoogleCredentials googleCredential = GoogleCredentials.fromStream(stream);
      googleCredential.createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
  
      AccessToken token = googleCredential.refreshAccessToken();
      String x = token.getTokenValue();
      return x;
  }</code></pre>
  </div>
  </div>
  </p>