Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Can't authenticate youtube api client with access token

My backend generates the access_token with below redirect controller:

const logged = async (req: Request, res: Response) => {
  const code = (req.query.code as string) || null;
  const state = (req.query.state as string) || null;
  const error = (req.query.error as string) || null;

  const oauth2Client = new youtube.auth.OAuth2(
    GOOGLE_CLIENT_ID,
    GOOGLE_SECRET,
    GOOGLE_REDIRECT_URI,
  );

  const { tokens: { access_token }} = await oauth2Client.getToken(code);
  res.send({ access_token });
};

Returned code is similar to: "ya29.<MUCH_MORE_CHARACTERS_HERE>_BtA0163"

Then somewhere else I'm trying to create a client with this access token like this:

const youtube = require("@googleapis/youtube");
const youtubeApi = youtube.youtube({
  version: "v3",
  auth: "ya29.<MUCH_MORE_CHARACTERS_HERE>_BtA0163",
});

(Btw I'm aware that I could get the client on the redirect controller. I just don't want to)

But everytime I try to access something, it gives me error:

  code: 400,
  errors: [
    {
      message: 'API key not valid. Please pass a valid API key.',
      domain: 'global',
      reason: 'badRequest'
    }
  ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe your goal is as follows.

  • You want to use your access token to the client with googleapis for Node.js.

In your script, how about the following modification?

From:

const youtube = require("@googleapis/youtube");
const youtubeApi = youtube.youtube({
  version: "v3",
  auth: "ya29.<MUCH_MORE_CHARACTERS_HERE>_BtA0163",
});

To:

const { google } = require("googleapis");
const auth = new google.auth.OAuth2();
auth.setCredentials({ access_token: "ya29.<MUCH_MORE_CHARACTERS_HERE>_BtA0163" });
const youtubeApi = google.youtube({ version: "v3", auth });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!