将 media_ids 添加到 Twitter API 调用会导致身份验证问题 [英] Adding media_ids to Twitter API call causes authentication issue

查看:32
本文介绍了将 media_ids 添加到 Twitter API 调用会导致身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的网页,可以通过位于 https://api.twitter.com/1.1/statuses/update.json 成功.我还可以使用 https://upload.twitter 将 JPG 图像上传到 Twitter.com/1.1/media/upload.json 并成功取回数字 media_id.我不能做的是将这两个放在一起创建一个图像推文:如果我添加 media_id,它会获得 HTTP 状态代码 401!

I have a simple web page which can create a text-only tweet through the Twitter REST API at https://api.twitter.com/1.1/statuses/update.json sucessfully. I am also able to upload a JPG image to Twitter with https://upload.twitter.com/1.1/media/upload.json and get back a numeric media_id successfully. What I cannot do is put these two together to create an image tweet: it gets HTTP status code 401 if I add in the media_id!

我正在使用 ASP.NET(我有 VB 和 C# 示例)但我希望问题不是特定于这种语言的.我正在更改的推文代码中仅有的两位是构建相同查询字符串"的位置:

I am using ASP.NET (I have both VB and C# examples) but I expect the problem is not specific to this language. The only two bits of the tweet code I am changing are where the same 'querystring' is built up:

var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}&media_ids={7}";
var baseString = string.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version, Uri.EscapeDataString(TweetText), TwitterMediaID);

var postBody = "status=" + Uri.EscapeDataString(TweetText) + "&media_ids=" + TwitterMediaID;
using (System.IO.Stream stream = request.GetRequestStream())
        {
            byte[] content = System.Text.ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

这一切真正做的是将 &media_ids=1104003004334784512 附加到字符串的末尾.我的其余代码没有改变.

All this really does is append &media_ids=1104003004334784512 to the end of the string. The rest of my code does not change.

我返回的 JSON 是:{"errors":[{"code":32, "message":"无法验证您的身份."}]}

The JSON I am returned is: {"errors":[{"code":32, "message":"Could not authenticate you."}]}

如果我删除 &media_ids=12345678901234567 则代码成功(HTTP 状态代码 200,并返回了大量有用的 JSON).

If I remove &media_ids=12345678901234567 then the code is successful (HTTP status code 200, and lots of useful JSON returned).

与纯文本推文相比,我认为我不需要任何特殊权限来创建图像推文.我读过各种博客,其中提到人们在使用 media_ids 时会出现间歇性 HTTP 401 错误,这与身份验证完全无关,但他们提供的解决方案都没有适用于我的站点.

I don't believe I need any special permissions to create an image tweet, compared to a text-only tweet. I have read various blogs which mention people had intermittent HTTP 401 errors when using media_ids, which were nothing to do with authentication at all, but none of the solutions they offer applied to my sitation.

文档表明我的 media_id 可以使用 2 个小时,那么当我的纯文本推文出现在我的 Twitter 个人资料上时,我怎么能把身份验证方面弄得一团糟?>

推荐答案

Twitter 要求 API 请求使用 OAuth 1.0a 身份验证.一个警告是必须以非常特定的格式创建 HMAC-SHA1 签名.

Twitter requires that API requests use OAuth 1.0a authentication. One caveat is that the HMAC-SHA1 signature must be created in a very specific format.

在这种情况下,它要求 baseFormat 按字母顺序排序 - 请参阅 https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html

In this case, it requires the baseFormat to be sorted alphabetically - see https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html

这篇关于将 media_ids 添加到 Twitter API 调用会导致身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆