2013-08-07 168 views
0

我正在開發一個windows phone 8應用程序,其中我需要整合twitter。所以我嘗試了很多我在搜索中獲得的方法。在Windows Phone 8應用程序中集成twitter

如果有人使用它,請幫助我。

在此先感謝。

編輯

using TweetSharp; 
using System.IO.IsolatedStorage; 
public partial class MainPage : PhoneApplicationPage 
{ 
    TwitterService service; 
    private const string consumerKey = "some Key"; 
    private const string consumerSecret = "Some key1"; 
    private OAuthRequestToken requestToken; 
    private OAuthAccessToken accesToken; 
    private bool userAuthenticated = false; 
    public MainPage() 
    { 
     InitializeComponent(); 
     service = new TwitterService(consumerKey, consumerSecret); 

     //Chek if we already have the Autehntification data 
     var token = getAccessToken(); 
     if (token != null) 
     { 
      service.AuthenticateWith(token.Token, token.TokenSecret); 
      userAuthenticated = true; 
     } 
    } 
private void tweetClick(object sender, RoutedEventArgs e) 
    { 
     if (userAuthenticated) 
      Tweet(Message.Text); 
     else 
      service.GetRequestToken(processRequestToken); 
    } 

    private void processRequestToken(OAuthRequestToken token, TwitterResponse response) 
    { 
     if (token == null) 
      Dispatcher.BeginInvoke(() => { MessageBox.Show("Error obtaining Request token"); }); 
     else 
     { 
      requestToken = token; 
      Dispatcher.BeginInvoke(() => 
      { 
       Browser.Visibility = System.Windows.Visibility.Visible; 
       Browser.Navigate(service.GetAuthorizationUri(requestToken)); 
      }); 
     } 
    } 

private void processAccessToken(OAuthAccessToken token, TwitterResponse response) 
    { 
     if (token == null) 
      Dispatcher.BeginInvoke(() => { MessageBox.Show("Error obtaining Access token"); }); 
     else 
     { 
      accesToken = token; 
      service.AuthenticateWith(token.Token, token.TokenSecret); 
      saveAccessToken(token); 
      userAuthenticated = true; 
      Dispatcher.BeginInvoke(() => 
      { 
       Tweet(Message.Text); 
      }); 
     } 
    } 


private void Tweet(string message) 
    { 
     service.SendTweet(message, tweetResponse); 
    } 

    private void tweetResponse(TwitterStatus tweet, TwitterResponse response) 
    { 
     if (response.StatusCode == HttpStatusCode.OK) 
     { 
      Dispatcher.BeginInvoke(() => { MessageBox.Show("Tweet posted successfully"); }); 
     } 
     else 
     { 
      if (response.StatusCode == HttpStatusCode.Unauthorized) 
      { 
       saveAccessToken(null); 
       userAuthenticated = false; 
       Dispatcher.BeginInvoke(() => { MessageBox.Show("Authentication error"); }); 
      } 
      else 
       Dispatcher.BeginInvoke(() => { MessageBox.Show("Error, please try again later"); }); 
     } 
    } 


private void BrowserNavitaged(object sender, System.Windows.Navigation.NavigationEventArgs e) 
    { 
     if (e.Uri.AbsoluteUri.Contains("oauth_verifier")) 
     { 
      var values = ParseQueryString(e.Uri.AbsoluteUri); 
      string verifier = values["oauth_verifier"]; 
      service.GetAccessToken(requestToken, verifier, processAccessToken); 
      Dispatcher.BeginInvoke(() => { Browser.Visibility = System.Windows.Visibility.Collapsed; }); 
     } 
    } 
private void saveAccessToken(OAuthAccessToken token) 
    { 
     if (IsolatedStorageSettings.ApplicationSettings.Contains("accessToken")) 
      IsolatedStorageSettings.ApplicationSettings["accessToken"] = token; 
     else 
      IsolatedStorageSettings.ApplicationSettings.Add("accessToken", token); 

     IsolatedStorageSettings.ApplicationSettings.Save(); 
    } 

    private OAuthAccessToken getAccessToken() 
    { 
     if (IsolatedStorageSettings.ApplicationSettings.Contains("accessToken")) 
      return IsolatedStorageSettings.ApplicationSettings["accessToken"] as OAuthAccessToken; 
     else 
      return null; 
    } 
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     if (Browser.Visibility == System.Windows.Visibility.Visible) 
     { 
      Browser.Visibility = System.Windows.Visibility.Collapsed; 
      e.Cancel = true; 
     } 
     base.OnBackKeyPress(e); 
    } 


    // From Hammock.Extensions.StringExtensions.cs 
    public static IDictionary<string, string> ParseQueryString(string query) 
    { 
     // [DC]: This method does not URL decode, and cannot handle decoded input 
     if (query.StartsWith("?")) query = query.Substring(1); 

     if (query.Equals(string.Empty)) 
     { 
      return new Dictionary<string, string>(); 
     } 

     var parts = query.Split(new[] { '&' }); 

     return parts.Select(
      part => part.Split(new[] { '=' })).ToDictionary(
       pair => pair[0], pair => pair[1] 
      ); 
    } 
} 

在這裏,我貼我試過的代碼。 M獲得「GetAccessToken」錯誤「不包含GetAccessToken的定義...」

+0

我已編輯我的問題,請檢查一次.. – user2428823

+0

IsolatedStorageSettings.ApplicationSettings.ContainsKey(「accessToken」)在您的getAccessToken()方法中執行此操作嗎? – xmashallax

+0

它是沒有采取「ContainsKey」,而是它只是顯示「包含」.. – user2428823

回答

1

您好最近我發佈了我的文章herehere too。它可能對您有所幫助

+0

嗨..非常感謝,現在它的工作很好.. – user2428823

+0

你好再次感謝你forthrth教程..但我得到這個錯誤「在Hammock.WindowsPhone.Mango.DLL中發生類型'System.ArgumentException'的異常,但未在用戶代碼中處理「任何幫助..? – veereev

相關問題