我正在开发一个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在&#34; GetAccessToken&#34;上收到错误说&#34;不包含GetAccessToken的定义...&#34;