我正在構建一個 twitter 機器人,它發送一個包含媒體的 twitter 帖子。我可以毫無問題地發送只包含文本的帖子。但是當我嘗試將 media_ids 添加到帖子時會引發以下錯誤:
[ { code: 32, message: 'Could not authenticate you.' } ]
這是我發送推特帖子的功能:
const axios = require('axios')
const fs = require('fs')
const Path = require('path')
const Twitter = require('twitter')
require('dotenv').config()
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})
async function sendTwitterPost() {
return new Promise(async (resolve, reject) => {
try {
const contents = await fs.promises.readFile('./temp/image.png', {encoding: 'base64'})
console.log("Got the image, uploading image to twitter...")
const res = await client.post('media/upload', {
media_data: contents,
media_category: 'tweet_image'
})
console.log(res.media_id " Image uploaded, sending twitter post...")
let mediaIds = []
mediaIds.push(res.media_id)
await client.post('statuses/update', {
status: 'New event occured!',
media_ids: mediaIds
})
resolve("Twitter post successfully sent")
}
catch(err){
console.log(err)
reject("Error while sending twitter post")
}
})
}
我的令牌具有讀寫權限,正如我所說,我可以發送僅包含文本的帖子。我也有更高的訪問權限。
uj5u.com熱心網友回復:
我通過改變來修復它
let mediaIds = []
mediaIds.push(res.media_id)
await client.post('statuses/update', {
status: 'New event occured!',
media_ids: mediaIds
})
到
await client.post('statuses/update', {
status: 'New event occured!',
media_ids: res.media_id_string
})
帶有 twitter 包的 twitter api v1 的 media_ids 欄位似乎需要該字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/412201.html
標籤:
