1

好的 twitter 对于社交媒体来说可能很棒,但对于我试图创建的一个小应用程序来说只是为了好玩已经变成了一场噩梦。我到处搜索,找不到关于如何使用 api 的很好的解释。

我正在使用 TweetSharp api,我想要做的就是让用户允许我的应用程序并能够从应用程序发布到他们的 Twitter。

当应用程序为用户输入密码时,我的问题就出现了……用户在哪里输入密码?好的,这就是我到目前为止所拥有的……如果有人能帮助我,我会非常感激,这样我就可以停止拉扯我的头发了……

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TweetSharp;
using System.Configuration;
using Hammock.Authentication.OAuth;
using System.Diagnostics;

namespace testtweet
{
    public partial class Form1 : Form
    {
        //I already have these values not showing here for obvious reasons.
        TwitterService service = new TwitterService("ConsumerKey", "ConsumerSecret");
        public Form1()
        {
            InitializeComponent();
            // Pass your credentials to the service

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

            // Step 2 - Redirect to the OAuth Authorization URL
            Uri uri = service.GetAuthorizationUri(requestToken);
            Form2 frm = new Form2(uri.ToString());
            frm.Show();

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

            // Step 4 - User authenticates using the Access Token
            service.AuthenticateWith(access.Token, access.TokenSecret);
            IEnumerable<TwitterStatus> mentions = service.ListTweetsMentioningMe();

        }

    }
}

在我的 Form2 上

public Form2(string url)
{
    InitializeComponent();
    webBrowser1.Navigate(url);

}

这是我迷路的地方。如您所见,第 3 步正在等待密码。我将如何将其设置在那里?我有一个想法直接写在 app.config 中,但是我怎么知道它是什么时候生成的呢?我应该制作第三种表格让用户在那里输入密码吗?

4

1 回答 1

2

所以这就是我所拥有的(在设计器中添加一个名为 webBrowser1 的网络浏览器)

public partial class TwitterBrowser : Form
{
    public TwitterBrowser()
    {
        InitializeComponent();

        webBrowser1.Navigated += OnNavigate;
    }

    private void OnNavigate(object sender, WebBrowserNavigatedEventArgs e)
    {
        if (e.Url.ToString() != @"https://api.twitter.com/oauth/authorize")
            return;

        if (webBrowser1.Document != null)
        {
            var regex = new Regex("<code>([0-9]*)</code>", RegexOptions.Multiline);
            VerificationCode = regex.Match(webBrowser1.DocumentText).Groups[1].Value;
            Close();
        }
    }

    public string VerificationCode { get; private set; }

    #region Implementation of ITwitterUserInteraction

    public void NavigateTo(Uri uri)
    {
        webBrowser1.Navigate(uri);
    }

    #endregion
}

然后像这样使用它:

  var browser = new TwitterBrowser();
  browser.NavigateTo(uri);
  browser.ShowDialog();
  result = browser.VerificationCode;
于 2011-12-16T16:19:28.923 回答