You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js mastodon-api包无法创建/删除Toot的问题求助

Mastodon API调用问题:无法创建/删除Toot(Node.js 'mastodon-api'包)

我在使用Node.js的mastodon-api包调用Mastodon基础API时遇到问题——无法删除Toot,同时也无法创建新Toot。以下是相关代码:

初始化及函数声明

const Mastodon = require('mastodon-api');
const mastodonInstance = 'https://social.example.com';
const clientId = '...';
const clientSecret = '...';
const accessToken = '...';
const profileId = '...';
let mastodon;

// Initialize Mastodon API
try {
    console.log(`Opening API instance to ${mastodonInstance}`);
    mastodon = new Mastodon({
        client_id: clientId,
        client_secret: clientSecret,
        access_token: accessToken,
        api_url: `${mastodonInstance}/api/v1/`
    });
} catch (error) {
    console.error('Opening of the Mastodon API instance failed');
    process.exit(1);
}

// createToot: create a new toot with specified toot and optional media
async function createToot(text, mediaData = null) {
    try {
        const params = { status: text };
        if (mediaData) {
            params.media = {
                file: mediaData.buffer,
                mime_type: mediaData.mimeType
            };
        }
        await mastodon.post('statuses', params);
        console.log('createToot: Toot successfully created.');
        return(0);
    } catch (error) {
        console.error('createToot: Error creating toot:', error.message);
    }
}

// deleteToot: delete existing toot
async function deleteToot(existingToot) {
    console.log('deleteToot: Hello!'); 
    try {
        const response = await mastodon.delete(`statuses/${existingToot.id}`);
        console.log('deleteToot: Server response:', response.data); 
        return(0);
    } catch (error) {
        console.error('deleteToot: Error:', error.message);
    }
}

函数调用方式

// create a new toot with the cleanText and mediaObject
    try{
        let status = createToot(cleanText,mediaObject);
    } catch(error) {
        console.error('main: Error creating new toot:', error.message);
        process.exit(1);
    };   

    // delete an existing toot specified by the existingToot object
    try {
        let status = await deleteToot(existingToot);
        console.log(`modifyToot: deleteToot returned ${status}`);
    } catch (error) {
        console.error(`modifyToot: deleteToot call failed. Error: ${error.message}`);
    }  

调用这两个函数时,控制台无任何console.log或console.error输出,Toot也没有任何变化——既未创建新Toot,也未删除原有Toot。

其他观察结果

  • 使用的mastodon-api版本为1.3.0,通过npm安装;
  • 可通过mastodon.get()成功读取Toot;
  • Mastodon服务器配置的客户端凭据已勾选read和write顶级权限;
  • 使用替代方法mastodon.del()时捕获到“方法不存在”的错误;
  • 尝试使用mastodon.deleteStatus(),无错误抛出但Toot未被删除。

尝试过的替代代码

try {
  await mastodon.del(`statuses/${Toot.id}`);
  console.log('Success: The status has been deleted.');
} catch (error) {
  console.error('Error:', error);
}

try {
  await mastodon.deleteStatus(Toot.id);
  console.log('Success: The status has been deleted.');
} catch (error) {
  console.error('Error:', error);
}

请问我的Mastodon API调用方式是否正确?


内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 02:52:17