Google Auth token from Chrome Extension with PassportJS returns 401 Unauthorized
<p>I'm trying to set up a Chrome Extension that uses <code>chrome.identity.getAuthToken</code> to get the logged in user's auth token and then use that to authenticate with an Express server using Passport and the <a href="https://github.com/robertdimarco/passport-google-token" rel="nofollow noreferrer"><code>passport-google-token</code></a> strategy.</p>

<p><code>getAuthToken</code> is giving me the token but when it's sent to my server, I'm getting a <code>401 unauthorized</code> error.</p>

<p>I'm pretty new to Passport and to token based authorisation in general, so I'm not sure if I've made a mistake or misunderstood how it's meant to work.</p>

<p>My chrome extension does this:</p>

<pre><code>chrome.identity.getAuthToken({"interactive": true}, function(token){
  var url = "http://localhost:30000/auth/chrome";
  var x = new XMLHttpRequest();
  x.open("GET", url);
  x.setRequestHeader('Authorization', "Bearer " + token);
  x.send();
});
</code></pre>

<p>and the token is being correctly passed into my callback.</p>

<p>I set up my Express server and Passport strategy like this:</p>

<pre><code>import * as express from "express";
import * as passport from "passport";
const GoogleTokenStrategy = require("passport-google-token").Strategy;

// set up Express and Passport...

passport.use(new GoogleTokenStrategy({
  clientID: --client id--,
  clientSecret: --client secret--
}, (accessToken, refreshToken, profile, done) =&gt; {
  return done(null, profile);
}));

app.get('/auth/chrome', passport.authenticate("google-token"), (req, res) =&gt; {
  res.send(req.user);
});
</code></pre>

<p>The client ID and secret come from the credentials I've set up at the Google API Manager:
<a href="https://i.stack.imgur.com/RvXBZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RvXBZ.png" alt="Google credentials"></a></p>

<p>If anyone can point me to what else I need to do or what I'm doing wrong, it would be much appreciated.</p>