无法使用 twitter v1.1 api 发布推文

问题描述

我正在尝试在没有 3rd 方软件包的客户端中发布推文。一世 尝试关注 twitter v1.1 文档,但仍然无法发布推文。 当我发布获取 400 代码一个错误对象时

代码:215 消息:“错误的身份验证数据”

我正在使用他们给我的正确密钥和秘密。我不是自己签名。 我试过了。

import crypto from "crypto"
import oauthSignature from "oauth-signature";

function App() {
    // consumer secret and token secret are down below
    const oauth_consumer_key = ""
    const oauth_token = ""
 
    // twitter docs says that should generate 32bytes of random data only containing words and numbers
    //but the example oauth_nonce they give is longer than that so 
    let generated_nonce = crypto.randomBytes(21).toString("hex")

    const url = "https://api.twitter.com/1.1/statuses/update.json?status=hi"
    // epoc time
    const pTime = Math.floor(new Date().getTime() / 1000)

    let httpMethod = "POST",parameters = {
            authorization: "OAuth",oauth_consumer_key: "",oauth_nonce: generated_nonce,oauth_signature_method: "HMAC-SHA1",oauth_token: "",oauth_timestamp: pTime,oauth_version: "1.0"
        },consumer_secret = "",tokenSecret = "",encodedSignature = oauthSignature.generate(httpMethod,url,parameters,consumer_secret,tokenSecret),const handleClick = async (e) => {
        e.preventDefault()
        try {
            const res = await fetch(`https://api.twitter.com/1.1/statuses/update.json?status=hi`,{
                method: 'POST',mode: "no-cors",headers: {
                    "authorization": "OAuth","oauth_consumer_key": oauth_consumer_key,"oauth_nonce": generated_nonce,"oauth_signature": encodedSignature,"oauth_signature_method": "HMAC-SHA1","oauth_timestamp": pTime,"oauth_token": oauth_token,"oauth_version": "1.0"
                }
            })
        } catch (e) {
            console.log(e.message);
        }
    }


    return (
        <div>
            <h1>React Twitter</h1>
            <label>Tweet</label>
            <button onClick={handleClick}>Post tweet</button>
        </div>
    );
}

export default App;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)