首页 > 解决方案 > 如何在 Twitter API 请求中使用承载代码?

问题描述

即使在生成承载代码后,我也无法从 twitter 检索数据。一个请求的例子是:

    var url = "https://api.twitter.com/1.1/statuses/show.json?id=1125750390664892417";
    var bearer = 'Bearer '+ 'myBearer code';
    fetch(url, {
    method: 'GET',
    withCredentials: true,
    credentials: 'include',
    headers: {
        'Authorization': bearer,
        'Content-Type': 'application/json'}
    }).then((anyResponse) => {
        console.log(anyResponse);
    }) .catch(error => console.log('Something bad happened ' + error.message)
    );

我不明白什么可能是错误的,但它总是失败。我在此页面(https://developer.twitter.com/en/docs/basics/authentication/guides/bearer-tokens)之后获得了不记名代码。但我没有对我的任何密钥进行编码(正如我在这里读到的那样,这是必要的,但 twitter 页面没有这么说)。有人知道我的请求可能出什么问题吗???

谢谢!

标签: javascriptrestapitwitter

解决方案


对我来说,当我使用没有 base64 加密的承载代码时,它终于在 Postman 中工作了。但是,它违背了上面用户评论的文档和漂亮的 python 编码。

我仍然没有在我的 JS 代码中实现,但由于它是 Postman 工作的,我可以想象任何具有相同凭据的 XHR 请求也应该工作。谢谢您的帮助!


推荐阅读