为什么我的手机无法扫描 Whatsapp API 生成的二维码

发布于 2025-01-17 05:55:55 字数 2233 浏览 3 评论 0原文

有很多关于二维码的建议,但我无法找到我自己问题的答案。所以我发布这个问题。

我正在尝试使用 Whatsapp-web.js,这是一个用于 NodeJS 的 WhatsApp 客户端库,通过 WhatsApp Web 浏览器应用程序进行连接。 下面是代码。

const fs = require('fs');
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');

// Path where the session data will be stored
const SESSION_FILE_PATH = './session.json';

// Load the session data if it has been previously saved
let sessionData;
if(fs.existsSync(SESSION_FILE_PATH)) {
    sessionData = require(SESSION_FILE_PATH);
}

// Use the saved values
const client = new Client({
    session: sessionData
});

// Save session values to the file upon successful auth
client.on('authenticated', (session) => {
    sessionData = session;
    fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
        if (err) {
            console.error(err);
        }
    });
});

client.on('qr', qr => {
    console.log(qr);
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
   
     // Number where you want to send the message.
    const number = "+911234567890";
   
     // Your message.
    const text = "Hey john";
   
     // Getting chatId from the number.
     // we have to delete "+" from the beginning and add "@c.us" at the end of the number.
    const chatId = number.substring(1) + "@c.us";
   
    // Sending message.
    client.sendMessage(chatId, text);
});

client.on('message', message => {
    console.log(message.body);
});

client.initialize();

问题是我的手机无法识别 Whatsapp 客户端生成的二维码。 我想知道 QR 图像生成器是否错误,因此我记录了 QR 字符串并将其粘贴到 QR 图像中生成工具来查看图像是否相同。对我来说,这些图像看起来不同。 但我不知道这是否是问题所在。

我想知道在用手机扫描二维码图像之前是否还有一个我忽略的步骤。目前,我无法登录,因为手机无法识别二维码图像。

编辑

二维码字符串为: 1@vFeiIVji3KTIwmxuzrWThEtLYAx1toBQ+gqPI/tpYwg7Nswvybj6z0XqIcKWAkIoQlaIG6+qAy 3yfw==,EMAQav3hXijRWQ0Fo54Vi7xINEhOa8ffU1i5tqAzK1s=,cI/NK6CodxcSeyf9niE3Cg==

代码生成的二维码图片为: https://i.sstatic.net/LHU1N.png

PS

有人请解决实际问题吗问题? 正如您从代码中看到的,我正在尝试使用 Whatsapp API,这需要身份验证。这是通过扫描 Whatsapp 客户端生成的二维码来实现的。这对我不起作用。

there are lots of advices on QR codes but I'm unable to find answers to my own problem. So I'm posting this question.

I'm trying to use Whatsapp-web.js, a WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app.
Below is the code.

const fs = require('fs');
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');

// Path where the session data will be stored
const SESSION_FILE_PATH = './session.json';

// Load the session data if it has been previously saved
let sessionData;
if(fs.existsSync(SESSION_FILE_PATH)) {
    sessionData = require(SESSION_FILE_PATH);
}

// Use the saved values
const client = new Client({
    session: sessionData
});

// Save session values to the file upon successful auth
client.on('authenticated', (session) => {
    sessionData = session;
    fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
        if (err) {
            console.error(err);
        }
    });
});

client.on('qr', qr => {
    console.log(qr);
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
   
     // Number where you want to send the message.
    const number = "+911234567890";
   
     // Your message.
    const text = "Hey john";
   
     // Getting chatId from the number.
     // we have to delete "+" from the beginning and add "@c.us" at the end of the number.
    const chatId = number.substring(1) + "@c.us";
   
    // Sending message.
    client.sendMessage(chatId, text);
});

client.on('message', message => {
    console.log(message.body);
});

client.initialize();

The problem is that the QR code generated by the Whatsapp client is not recognized by my phone.
I wondered if the QR image generator was wrong, so I logged the QR string and pasted it into a QR image generating tool to see if the images were identical. To me the images looked different.
But I don't know if that's the problem at all.

I wish to know if there is an additional step I have overlooked before I can scan the QR image with my phone. Currently, I cannot login because phone doesn't recognize the QR image.

Edit

The QR code string is:
1@vFeiIVji3KTIwmxuzrWThEtLYAx1toBQ+gqPI/tpYwg7Nswvybj6z0XqIcKWAkIoQlaIG6+qAy3yfw==,EMaQav3hXijRWQ0Fo54Vi7xINEhOa8ffU1i5tqAzK1s=,cI/NK6CodxcSeyf9niE3Cg==

The QR code image generated by the code is:
https://i.sstatic.net/LHU1N.png

PS

Would anybody please address the actual problem?
As you can see from the code, I'm trying to use Whatsapp API and that requires authentication. This is achieved by scanning the QR code generated by the Whatsapp client. It's not working for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文