2011-12-16 17 views
1

好的twitter對社交媒體來說可能很棒,但對於一個我試圖爲了娛樂而創建的小應用已經變成了一場噩夢。我到處搜索,無法找到關於如何使用API​​的很好的解釋。Twitter和Winform C#應用讓我變得禿頭

我正在使用TweetSharp api,我想要做的就是讓用戶允許我的應用程序並能夠從應用程序發佈到他們的Twitter。

我的問題是當應用程序給出用戶輸入的密碼時......用戶在哪裏輸入密碼?好了,所以這是我有這麼far..if有人可以幫助我將不勝感激,所以我可以停止拉我的頭髮..

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中編寫,但我怎麼知道它什麼時候生成的?我應該製作第三種表單讓用戶在其中輸入PIN碼嗎?

+4

+1爲標題(即使它可能可能是更多的指示你的實際問題=)) – jadarnel27 2011-12-16 15:57:24

+0

謝謝!是啊沒有想到這個......我應該繼續改變它嗎? – Andres 2011-12-16 16:00:46

回答

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;