Twitter 和 Winform C# 应用程序让我秃头

发布于 2024-12-21 09:04:59 字数 2096 浏览 2 评论 0原文

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

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

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

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 步正在等待 PIN 码。我该如何将其设置在那里?我有一个想法直接写在app.config中但是 我如何知道它何时生成?我应该制作第三个表单让用户在那里输入 pin 码吗?

Ok twitter might be great for social media but for a small app that I am trying to create just for fun has turned into a nightmare. I have searched everywhere and can't find a good explanation on how to use the api.

I am using the TweetSharp api and all I want to do is for user to allow my app and be able to post from the app to their twitter.

My problem comes when the app gives out a pin code for the user to type in...where would the user type it in at? Ok so this is what I have so far..if anyone can help i would be most greatful so I can stop pulling my hair..

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();

        }

    }
}

on my Form2

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

}

Here is where I am lost. As you see Step 3 is wating for the pin code..How would I set it in there? I had an idea to write directly in the app.config but
how do I know when it has been generated? Should I make a 3rd form to have the user input the pin code there?

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

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

发布评论

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

评论(1

放手` 2024-12-28 09:04:59

这就是我所得到的(在设计器中添加一个名为 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;

so this is what I've got (add a webbrowser named webBrowser1 in the designer)

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
}

Then use it like this:

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