Reputation: 563
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'
}
]
Upvotes: 0
Views: 547
Reputation: 201398
I believe your goal is as follows.
In your script, how about the following modification?
const youtube = require("@googleapis/youtube");
const youtubeApi = youtube.youtube({
version: "v3",
auth: "ya29.<MUCH_MORE_CHARACTERS_HERE>_BtA0163",
});
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 });
Upvotes: 1