0

I am developing an windows phone 8 application, in which I need to integrate the twitter. So I tried many ways which I got in my search.

So please help me out if anyone used it.

Thanks in advance.

EDIT

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]
            );
    }
}

Here I posted the code which I tried. M getting an error on "GetAccessToken" saying that "does not contain a definition for GetAccessToken... "

4

0 回答 0