为什么 TweetSharp 会抛出异常?

发布于 2024-10-08 20:52:24 字数 1340 浏览 2 评论 0原文

这是最基本的示例,它抛出异常。

// Pass your credentials to the service
TwitterService service = new TwitterService(API_KEY,API_SECRET);
service.UserAgent = "StackBot";

// Step 1 - Retrieve an OAuth Request Token
OAuthRequestToken request = service.GetRequestToken();

// Step 2 - Redirect to the OAuth Authorization URL
Uri uri = service.GetAuthorizationUri(request);
Console.WriteLine(uri.ToString());

// Step 3 - Exchange the Request Token for an Access Token
string verifier = "123456"; // <-- This is input into your application by your user
OAuthRequestToken requestToken = new OAuthRequestToken();
OAuthAccessToken access = service.GetAccessToken(requestToken, verifier);

以下是例外情况:

Unhandled Exception: System.ArgumentNullException: Argument cannot be null.
Parameter name: query
  at System.Compat.Web.HttpUtility.ParseQueryString (System.String query, System.Text.Encoding encoding) [0x00000] in :0 
  at System.Compat.Web.HttpUtility.ParseQueryString (System.String query) [0x00000] in :0 
  at TweetSharp.TwitterService.GetRequestToken (System.String callback) [0x00000] in :0 
  at TweetSharp.TwitterService.GetRequestToken () [0x00000] in :0 
  at Namespace.Class.Main (System.String[] args) [0x00049] in Main.cs:179

注意:我在 Ubuntu 10.10 64 位上使用 Mono 2.6。

This is the most BASIC example possible and it's throwing an exception.

// Pass your credentials to the service
TwitterService service = new TwitterService(API_KEY,API_SECRET);
service.UserAgent = "StackBot";

// Step 1 - Retrieve an OAuth Request Token
OAuthRequestToken request = service.GetRequestToken();

// Step 2 - Redirect to the OAuth Authorization URL
Uri uri = service.GetAuthorizationUri(request);
Console.WriteLine(uri.ToString());

// Step 3 - Exchange the Request Token for an Access Token
string verifier = "123456"; // <-- This is input into your application by your user
OAuthRequestToken requestToken = new OAuthRequestToken();
OAuthAccessToken access = service.GetAccessToken(requestToken, verifier);

Here is the exception:

Unhandled Exception: System.ArgumentNullException: Argument cannot be null.
Parameter name: query
  at System.Compat.Web.HttpUtility.ParseQueryString (System.String query, System.Text.Encoding encoding) [0x00000] in :0 
  at System.Compat.Web.HttpUtility.ParseQueryString (System.String query) [0x00000] in :0 
  at TweetSharp.TwitterService.GetRequestToken (System.String callback) [0x00000] in :0 
  at TweetSharp.TwitterService.GetRequestToken () [0x00000] in :0 
  at Namespace.Class.Main (System.String[] args) [0x00049] in Main.cs:179

Note: I'm using Mono 2.6 on Ubuntu 10.10 64-bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-10-15 20:52:24

对于初学者,您要创建一个新的“requestToken”变量,一个空变量,具有 null 值,然后尝试使用它来获取访问令牌。您需要使用从 GetRequestToken 调用中实际收到的令牌(您称为“请求”的令牌)。

For starters you're creating a new 'requestToken' variable, an empty one, with null values, and then trying to use that to get an access token. You need to use the one you actually received from the call to GetRequestToken (the one you call 'request').

花之痕靓丽 2024-10-15 20:52:24

我遇到了同样的问题,我自己无法重现它,但它会在某些客户端的服务器端定期发生。堆栈跟踪和错误描述与您得到的类似:

InnerException:值不能为空。参数名称:查询于
System.Compat.Web.HttpUtility.ParseQueryString(字符串查询,编码
编码)在 System.Compat.Web.HttpUtility.ParseQueryString(字符串
查询)在 TweetSharp.TwitterService.GetRequestToken(字符串回调)

我查看了 TweetSharp 代码,现在它可能有点过时了(v.2.3)。请告诉我该问题是否在新版本中得到解决。似乎这里发生了错误:

    var response = _oauth.Request(request);
    SetResponse(response);
    var query = HttpUtility.ParseQueryString(response.Content);

似乎对 twitter 服务器的请求由于某种原因失败了,但是没有检查 response.Content 是否为空。然后 ParseQueryString 抛出异常。

我的解决方案只是忽略此错误并允许用户重试。看来网络错误、代理服务器或不稳定的连接可能会导致此错误。

I had the same issue, can't reproduce it myself but it happens periodically on server side with some clients. The stack trace and error description is similar of what you got:

InnerException: Value cannot be null. Parameter name: query at
System.Compat.Web.HttpUtility.ParseQueryString(String query, Encoding
encoding) at System.Compat.Web.HttpUtility.ParseQueryString(String
query) at TweetSharp.TwitterService.GetRequestToken(String callback)

I looked into TweetSharp code, it may be a bit obsolete nowadays (v. 2.3). Please let me know if the issue fixed in newer versions. It seems that error happens here:

    var response = _oauth.Request(request);
    SetResponse(response);
    var query = HttpUtility.ParseQueryString(response.Content);

It seems that request to twitter server failed for whatever reason, but there's no check if response.Content is empty. Then ParseQueryString throw exception.

My solution was just to ignore this error and allow user to try again. It seems that network error, proxy server or unstable connection may cause this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文