Search code examples
node.jsyoutubeyoutube-apiyoutube-data-api

google oauth2 suddenly failing with Malformed auth code error


I am trying to write a javascript file that uses the youtube v3 api.

i created a google cloud project, enable youtube api, and go through the credentials creation process, which at the end leads me to download a json file with my client id etc... enter image description here

The resulting 'client_secret.json' file looks like this:

{
    "web": {
        "client_id": "aaaaaa",
        "project_id": "aaaa",
        "auth_uri": "aaaa",
        "token_uri": "aaaa": "https://www.googleapis.com/oauth2/v1/certs",
        "client_secret": "aaaaa",
        "redirect_uris": [
            "http://localhost:3030/"
        ]
    }
}

I have a file titled 'attempt.js' :

const { google } = require('googleapis');
const readline = require('readline');
const fs = require('fs');

// YouTube API credentials
var CLIENT_ID = '';
var CLIENT_SECRET = '';
var REDIRECT_URL = '';
var SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'];

main()
async function main() {

    console.log('set auth vars from json file')
    await getAuthVars()
    async function getAuthVars() {
        return new Promise(async function (resolve, reject) {
            fs.readFile('client_secret.json', function processClientSecrets(err, content) {
                if (err) {
                    console.log('Error loading client secret file: ' + err);
                    return;
                }
                content = JSON.parse(content)
                console.log('content=', content)
                CLIENT_SECRET = content.web.client_secret;
                CLIENT_ID = content.web.client_id;
                REDIRECT_URL = content.web.redirect_uris[0];
                resolve()
            });
        })
    }

    console.log('begin auth flow')
    beginAuthFlow()
    async function beginAuthFlow() {

        // Create an OAuth2 client
        const oAuth2Client = new google.auth.OAuth2(
            CLIENT_ID,
            CLIENT_SECRET,
            REDIRECT_URL
        );

        // Generate the authentication URL
        const authUrl = oAuth2Client.generateAuthUrl({
            access_type: 'offline',
            scope: SCOPES
        });

        // Create an interface for reading user input
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });

        // Ask the user to visit the authentication URL and provide the authorization code
        console.log('Authorize this app by visiting the following URL:');
        console.log(authUrl);
        rl.question('Enter the authorization code: ', (code) => {
            rl.close();
            // Exchange authorization code for access token
            oAuth2Client.getToken(code, (err, token) => {
                if (err) {
                    console.error('Error retrieving access token:', err);
                    return;
                }

                // Set the access token for future requests
                oAuth2Client.setCredentials(token);

                // Create a YouTube service client
                const youtube = google.youtube({
                    version: 'v3',
                    auth: oAuth2Client
                });

                // Retrieve details about the authenticated user's YouTube account
                youtube.channels.list(
                    {
                        mine: true,
                        part: 'snippet,contentDetails,statistics'
                    },
                    (err, res) => {
                        if (err) {
                            console.error('Error retrieving channel details:', err);
                            return;
                        }

                        // Display the channel information
                        const channel = res.data.items[0];
                        console.log('Channel ID:', channel.id);
                        console.log('Channel Title:', channel.snippet.title);
                        console.log('Subscriber Count:', channel.statistics.subscriberCount);
                        console.log('View Count:', channel.statistics.viewCount);
                        console.log('Video Count:', channel.statistics.videoCount);
                    }
                );
            });
        });
    }
}

When I run this file with node attempt.js it creates the URL correctly, which I think is good because it means my auth stuff is working:

begin auth flow
Authorize this app by visiting the following URL:
https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=aaaaaa&response_type=code&client_id=aaaaaaa&redirect_uri=http%3A%2F%2Flocalhost%3A3030%2F
Enter the authorization code: 

I go to the URL, sign into a youtube account, and it redirects to a url like so:

http://localhost:3031/?code=4%2F0aaaaaa

So I copy everything after the 'code=' string, so the following text is in my copy clipboard: 4%2F0aaaaaa

I paste that in my terminal window and hit enter, which gives me this error:

Error retrieving access token: GaxiosError: invalid_grant

data: {
      error: 'invalid_grant',
      error_description: 'Malformed auth code.'
    },

What am i doing wrong?


Solution

  • code that i copied from browser url had to be decoded in script:

    oAuth2Client.getToken(decodeURIComponent(code), (err, token) => {
    

    works