Whatsapp API 不允许我下载音频

问题描述 投票:0回答:1

我一直在尝试从 Whatsapp API 下载音频,但是无论我如何尝试,它都不会让我这么做。

我首先调用来检索音频的 URL,效果很好,但是当我尝试下载音频时,它总是出错。

我尝试过使用 fetch 和 axios,在使用 Postman 后,我让它在那里工作,我可以看到我的音频,但我还没有设法让它在 Node 中工作。

我尝试过使用许多不同的标题,但无济于事。 通常我会收到 400 错误,但在处理标题时我设法得到“获取失败”错误,但我有点卡在那里,不知道还能尝试什么。

还有其他类似的问题,但我已经尝试过他们的答案,但没有成功。

我的代码:

async function downloadAudio(url,outputPath, audioType){
 
    var file = fs.createWriteStream(path.join(outputPath, `audio.${audioType}`) );

    try {
        var response = await fetch(url, {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'Host': new URL(url).host,
                'User-Agent': 'PostmanRuntime/7.42.0',
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate, br',
                'Connection': 'keep-alive',
                'Transfer-Encoding': 'chunked',
                'Content-Type': 'audio/ogg',
            },
        });

        if (!response.ok) {
            throw new Error(`Failed to get '${url}' (${response.status})`);
        }  

        response.body.pipe(file);

        return new Promise((resolve, reject) => {
            file.on('finish', () => {
                file.close();
                console.log(`Downloaded audio to ${outputPath}`);
                resolve();
            });

            file.on('error', (err) => {
                fs.unlink(outputPath, () => {}); 
                reject(err);
            });
        });
    } catch (error) {
        console.error(`Error downloading the audio: ${error.message} \n${error}`);
    }


}
javascript node.js whatsapp whatsapp-cloud-api
1个回答
0
投票

您正在使用

Content-Type: audio/ogg
发送请求,但您 发送任何音频,您正在尝试接收它。最有可能的是,服务器会通过进行一些初步检查来丢弃您的请求。
因此,请尝试准确复制 Postman 在工作请求中自动添加的标头,或者尝试删除您的
Content-Type
标头。
旁注:通常,a
GET
请求中没有正文数据,也没有 Content-Type。

© www.soinside.com 2019 - 2024. All rights reserved.