Google OAuth2 突然出现 “Malformed auth code error” 错误。

huangapple go评论68阅读模式
英文:

google oauth2 suddenly failing with Malformed auth code error

问题

我正在尝试编写一个使用YouTube v3 API的JavaScript文件。

我创建了一个Google Cloud项目,启用了YouTube API,并经历了凭据创建过程,最终让我下载一个包含客户端ID等信息的JSON文件...

生成的 'client_secret.json' 文件如下:

{
    "web": {
        "client_id": "aaaaaa",
        "project_id": "aaaa",
        "auth_uri": "aaaa",
        "token_uri": "aaaa",
        "client_secret": "aaaaa",
        "redirect_uris": [
            "http://localhost:3030/"
        ]
    }
}

我有一个名为 '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'];

// 主函数
async function main() {
    console.log('从JSON文件设置认证变量');
    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('加载客户端秘钥文件时出错: ' + err);
                    return;
                }
                content = JSON.parse(content);
                CLIENT_SECRET = content.web.client_secret;
                CLIENT_ID = content.web.client_id;
                REDIRECT_URL = content.web.redirect_uris[0];
                resolve();
            });
        });
    }

    console.log('开始认证流程');
    beginAuthFlow();

    // 开始认证流程的函数
    async function beginAuthFlow() {
        // 创建OAuth2客户端
        const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

        // 生成认证URL
        const authUrl = oAuth2Client.generateAuthUrl({
            access_type: 'offline',
            scope: SCOPES
        });

        // 创建用于读取用户输入的界面
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });

        // 要求用户访问认证URL并提供授权码
        console.log('通过访问以下URL授权此应用程序:');
        console.log(authUrl);
        rl.question('输入授权码:', (code) => {
            rl.close();
            // 交换授权码以获取访问令牌
            oAuth2Client.getToken(code, (err, token) => {
                if (err) {
                    console.error('检索访问令牌时出错:', err);
                    return;
                }

                // 设置访问令牌以供将来的请求使用
                oAuth2Client.setCredentials(token);

                // 创建一个YouTube服务客户端
                const youtube = google.youtube({
                    version: 'v3',
                    auth: oAuth2Client
                });

                // 检索已认证用户的YouTube帐户的详细信息
                youtube.channels.list(
                    {
                        mine: true,
                        part: 'snippet,contentDetails,statistics'
                    },
                    (err, res) => {
                        if (err) {
                            console.error('检索频道详细信息时出错:', err);
                            return;
                        }

                        // 显示频道信息
                        const channel = res.data.items[0];
                        console.log('频道ID:', channel.id);
                        console.log('频道标题:', channel.snippet.title);
                        console.log('订阅者数量:', channel.statistics.subscriberCount);
                        console.log('观看次数:', channel.statistics.viewCount);
                        console.log('视频数量:', channel.statistics.videoCount);
                    }
                );
            });
        });
    }
}

// 运行主函数
main();

当我使用 node attempt.js 运行此文件时,它会正确地创建URL,这表明我的认证步骤有效。

然后,我访问URL,登录到YouTube帐户,并重定向到一个URL,如下所示:

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

然后,我将 'code=' 字符串后的所有文本复制到剪贴板,因此我的剪贴板中有以下文本:4%2F0aaaaaa

然后,我将其粘贴到终端窗口并按回车键,但会收到错误消息:

检索访问令牌时出错:GaxiosError: invalid_grant
data: {
error: 'invalid_grant',
error_description: 'Malformed auth code.'
},

请问我做错了什么?

英文:

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...
Google OAuth2 突然出现 “Malformed auth code error” 错误。

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?

答案1

得分: 1

从浏览器URL复制的代码必须在脚本中解码:

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


有效
英文:

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

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

works

huangapple
  • 本文由 发表于 2023年6月8日 13:20:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定