asp.net c#未提供所需的oauth_verifier参数

时间:2014-11-28 13:45:50

标签: c# twitter linq-to-twitter

我正面临这个错误,现在正在使用linq到twitter库:

<?xml version="1.0" encoding="UTF-8"?> 
<hash> 
<error>Required oauth_verifier parameter not provided</error> 
<request>/oauth/access_token</request> 
</hash> 

我已按照此文档链接:https://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%20ASP.NET%20Web%20Forms&referringTitle=Learning%20to%20use%20OAuth 为了实现OAuth流程,我在以下行中收到此错误: await auth.CompleteAuthorizeAsync(new Uri(twitterCallbackUrl));

下面是完整的代码,请帮助我:

AspNetAuthorizer auth;
    string twitterCallbackUrl = "http://127.0.0.1:58192/Default.aspx";

    protected async void Page_Load(object sender, EventArgs e)
    {
        auth = new AspNetAuthorizer
        {
            CredentialStore = new SessionStateCredentialStore
            {
                ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
            },
            GoToTwitterAuthorization =
                twitterUrl => Response.Redirect(twitterUrl, false)
        };

        if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
        {
            __await auth.CompleteAuthorizeAsync(new Uri(twitterCallbackUrl));__

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database, isolated 
            //   storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent queries 
            //   to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let you 
            //   make queries without re-authorizing.
            //
            var credentials = auth.CredentialStore;
            string oauthToken = credentials.OAuthToken;
            string oauthTokenSecret = credentials.OAuthTokenSecret;
            string screenName = credentials.ScreenName;
            ulong userID = credentials.UserID;

            //Response.Redirect("~/Default.aspx", false);
        }
    }

    protected async void AuthorizeButton_Click(object sender, EventArgs e)
    {
        await auth.BeginAuthorizeAsync(new Uri(twitterCallbackUrl));
        //await auth.BeginAuthorizeAsync(Request.Url);
    }

1 个答案:

答案 0 :(得分:2)

出现此问题的原因是您的自定义网址未包含Twitter在应用程序请求授权后返回的参数。如果在CompleteAuthorizationAsync上设置断点并在立即窗口中键入Request.Url,则会看到以下参数:

如果您仍想手动指定网址,则需要包含这些参数。这是一种方法:

    string completeOAuthUrl = twitterCallbackUrl + Request.Url.Query;
    await auth.CompleteAuthorizeAsync(completeOAuthUrl);

或者,您可以使用页面URL,因为它已包含正确的参数:

    await auth.CompleteAuthorizeAsync(Request.Url);