C# Twitter API + OAuth:无法发送状态更新,收到 401
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twitter API + OAuth: Can't send status updates, getting 401
提问by Sunday Ironfoot
I'm trying to use Twitter's API and OAuth to send status updates (new Tweets). I am using Shannon Whitley .NET code example http://www.voiceoftech.com/swhitley/?p=681(as recommended on the Twitter API docs). I can read (GET) using OAuth just fine, however when I try to send a status update via http ://twitter.com/statuses/update.xml (using a POST), it returns a 401 with the following XML:
我正在尝试使用 Twitter 的 API 和 OAuth 发送状态更新(新推文)。我正在使用 Shannon Whitley .NET 代码示例http://www.voiceoftech.com/swhitley/?p=681(如 Twitter API 文档中所推荐)。我可以使用 OAuth 读取 (GET) 就好了,但是当我尝试通过 http://twitter.com/statuses/update.xml(使用 POST)发送状态更新时,它返回一个带有以下 XML 的 401:
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/statuses/update.xml</request>
<error>Read-only application cannot POST</error>
</hash>
I swear I have set up my application to use read and write, the authorise page at Twitter (http ://twitter.com/oauth/authorize) even says "The application TweeVerbs.com (Development) by would like the ability to access and updateyour data on Twitter."
我发誓我已经将我的应用程序设置为使用读写,Twitter 上的授权页面 (http://twitter.com/oauth/authorize) 甚至说“应用程序 TweeVerbs.com(开发)希望能够访问并在 Twitter 上更新你的数据。”
Yet it's still saying "Read-only application cannot POST". WTF!?
然而它仍然说“只读应用程序不能发布”。卧槽!?
I have googled this error message until I was blue in the face. I found somewhere that said to add the querystring paremeter oauth_access_type=writeto the redirect URL which goes to the Twitter authorise page which I have done, but it still gives me a 401.
我在谷歌上搜索了这条错误信息,直到我脸色发青。我发现某处说要将查询字符串参数oauth_access_type=write添加到重定向 URL,该 URL 转到我已经完成的 Twitter 授权页面,但它仍然给我 401。
If it helps, here is the data that is getting sent back and firth as per the OAuth workflow:
如果有帮助,这里是根据 OAuth 工作流程发回和发送的数据:
Request Authorise Token:
请求授权令牌:
http ://twitter.com/oauth/request_token?oauth_consumer_key=tViV8vAt4cqSKbGdPGWT7Q&oauth_nonce=2790042&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1244567068&oauth_version=1.0&oauth_signature=KzxcXN%2bQ0AJoAJ%2flQfzs8SLjC%2fQ%3d
http://twitter.com/oauth/request_token?oauth_consumer_key=tViV8vAt4cqSKbGdPGWT7Q&oauth_nonce=2790042&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1244567068&oauth_timestamp=1244567068&oauth_timestamp=1244567068&oauth%2fj%2fj%2fj%2aqsl%2fj%2fj%2fslqsl&oauthsl&oauthsl&oauth_sl
Generated Authorize Redirect URL:
生成的授权重定向 URL:
http ://twitter.com/oauth/authorize?oauth_token=EpyBg3nJGOmtmBjRUAsqqaGHARb2F2F2VcccqHkwio&oauth_access_type=write
http://twitter.com/oauth/authorize?oauth_token=EpyBg3nJGOmtmBjRUAsqqaGHARb2F2F2VcccqHkwio&oauth_access_type=write
Authorise Screen Message:"The application TweeVerbs.com (Development) by would like the ability to access and update your data on Twitter. This application plans to use Twitter for logging you in in the future. Sign out if you want to connect to an account other than Sironfoot."
授权屏幕消息:“应用程序 TweeVerbs.com(开发)希望能够访问和更新您在 Twitter 上的数据。此应用程序计划在将来使用 Twitter 来登录您。如果您想连接到Sironfoot 以外的帐户。”
Get Access Token:
获取访问令牌:
http ://twitter.com/oauth/access_token?oauth_consumer_key=tViV8vAt4cqSKbGdPGWT7Q&oauth_nonce=2016804&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1244567166&oauth_token=EpyBg3nJGOmtmBjRUAsqqaGHARb2F2F2VcccqHkwio&oauth_version=1.0&oauth_signature=%2bEVQUxUPLT%2b%2bkfaG0Vq1YJZXcAw%3d
HTTP://twitter.com/oauth/access_token oauth_consumer_key = tViV8vAt4cqSKbGdPGWT7Q&oauth_nonce = 2016804&oauth_signature_method = HMAC-SHA1&oauth_timestamp = 1244567166&组oauth_token = EpyBg3nJGOmtmBjRUAsqqaGHARb2F2F2VcccqHkwio&oauth_version = 1.0&oauth_signature =%2bEVQUxUPLT%2B%2bkfaG0Vq1YJZXcAw%3D
Status Update API call:URL- http ://twitter.com/statuses/update.xml POST data- oauth_consumer_key=tViV8vAt4cqSKbGdPGWT7Q&oauth_nonce=5707692&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1244567268&oauth_token=19130957-nb89DjZhjCAzcbHUa96yRWHqlQFQIJ0AKyXpqnHt1&oauth_version=1.0&status=HelloWorld&oauth_signature=WqA%2bWY0IxveeSJ7G3Ewy3whh1sE%3d
状态更新API调用:URL- HTTP://twitter.com/statuses/update.xml POST数据- oauth_consumer_key = tViV8vAt4cqSKbGdPGWT7Q&oauth_nonce = 5707692&oauth_signature_method = HMAC-SHA1&oauth_timestamp = 1244567268&组oauth_token = 19130957-nb89DjZhjCAzcbHUa96yRWHqlQFQIJ0AKyXpqnHt1&oauth_version = 1.0&状态=的HelloWorld&oauth_signature = WQA%2bWY0IxveeSJ7G3Ewy3whh1sE%3D
采纳答案by Sunday Ironfoot
Dunno what the problem was, but I deleted my Application registration on Twitter (You have to register apps in Twitter to get OAuth keys etc.), and then recreated it. Now it works fine. Weird, probably a problem with Twitter screwing up.
不知道问题是什么,但我删除了我在 Twitter 上的应用程序注册(您必须在 Twitter 中注册应用程序才能获取 OAuth 密钥等),然后重新创建它。现在它工作正常。奇怪,可能是 Twitter 搞砸的问题。
I'm also using Tweetsharp (http://tweetsharp.com/), highly recommend it, it's got a nice fluent API. Note: switching over to Tweetsharp wasn't the fix, I had already switched over and had the same problem until I delete and recreated app registration on Twitter.
我也在使用 Tweetsharp ( http://tweetsharp.com/),强烈推荐它,它有一个很好的流畅的 API。注意:切换到 Tweetsharp 不是解决方案,我已经切换并遇到了同样的问题,直到我在 Twitter 上删除并重新创建应用程序注册。
回答by Proxdeveloper
I've dont that several times now, without succes, I'm getting a 401 exception while requesting an acces token from twitter.
我现在没有多次这样做,但没有成功,我在从 twitter 请求访问令牌时收到 401 异常。
I'm using Tweetsharp as well as you.
我和你一样在使用 Tweetsharp。
回答by abraham
If you authorized your application to access your Twitter account when the application was set in "read only access" mode and you get the "Read-only application cannot POST" error after changing the application settings to be "read and write access" then you have to revoke access to the application on https://twitter.com/settings/connectionsand then reauthorize it. The "read only" access_token tends to be sticky until you revoke it.
如果您在应用程序设置为“只读访问”模式时授权应用程序访问您的 Twitter 帐户,并且在将应用程序设置更改为“读写访问”后收到“只读应用程序无法发布”错误,那么您必须在https://twitter.com/settings/connections上撤销对应用程序的访问权限,然后重新授权。“只读” access_token 在您撤销它之前往往是粘性的。
回答by hira
plus also see if your system's CLOCK is synched with internet; on windows you can do it by adjusting date time settings
另外还要查看您系统的时钟是否与互联网同步;在 Windows 上,您可以通过调整日期时间设置来实现
because if you CLOCK is not synched you will get 401 unauthorized exception; best of luck cheers
因为如果您的 CLOCK 未同步,您将收到 401 未经授权的异常;祝你好运
回答by Waqas
- Go to http://www.twitter.com/oauth_clients
- Select your App.
- Edit Application Setting.
- Set Default Access type to Read & Write.
- Save & Run.
- 转到http://www.twitter.com/oauth_clients
- 选择您的应用程序。
- 编辑应用程序设置。
- 将默认访问类型设置为读取和写入。
- 保存并运行。
Hope this helps.
希望这可以帮助。