I have been trying for several days to get OAuth to work with Twitter in my Windows Phone application, but all the information I find is outdated or hard to follow. In the end, I got somewhere when I found this blog post http://samjarawan.blogspot.co.uk/2010/09/building-real-windows-phone-7-twitter_18.html , which delivered me all the way to access the token, after which it failed.
My code is almost identical to the one on the blog, pretty much just changing the consumer key and consumer secret. Even their application does not work. It perfectly displays the Twitter login screen and authenticates successfully, but in the RequestAccessToken function it fails:
if (String.IsNullOrEmpty(twitteruser.AccessToken) || String.IsNullOrEmpty(twitteruser.AccessTokenSecret)) { Dispatcher.BeginInvoke(() => MessageBox.Show(response.Content)); return; }
In fact, it is very unpleasant that the Unicode () replacement symbol is displayed in the message window and nothing more. I also checked response.StatusCode, and everything is in order, so I can not say about the error.
If someone can help me with this, it will be great. I saw other tutorials that require entering the user type in the PIN, but I could not get them to work.
EDIT: I just tried getting TweetSharp to work, but once again it was not able to get the access token. Here is the code I use for TweetSharp:
public partial class TwitterAuthorisationPage : PhoneApplicationPage { private const string consumerKey = "myKey"; private const string consumerSecret = "mySecret"; // These are the correct values for my app private const string requestTokenUri = "https://api.twitter.com/oauth/request_token"; private const string oAuthVersion = "1.0a"; private const string authorizeUri = "https://api.twitter.com/oauth/authorize"; private const string accessTokenUri = "https://api.twitter.com/oauth/access_token"; private const string callbackUri = "http://bing.com"; private TwitterService twitterService = new TwitterService(consumerKey, consumerSecret); private OAuthRequestToken _requestToken = null; public TwitterAuthorisationPage() { InitializeComponent(); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); twitterService.GetRequestToken((requestToken, response) => { if (response.StatusCode == HttpStatusCode.OK) { _requestToken = requestToken; Dispatcher.BeginInvoke(() => BrowserControl.Navigate(twitterService.GetAuthorizationUri(requestToken))); } else { Dispatcher.BeginInvoke(() => MessageBox.Show("Failed to connect to Twitter. Please try again.\n" + response.StatusDescription)); } }); } private void ConfirmButton_Click(object sender, RoutedEventArgs e) { twitterService.GetAccessToken(_requestToken, PINEntry.Text, (accessToken, response) => { if (response.StatusCode == HttpStatusCode.OK) { //These lines just print ? System.Diagnostics.Debug.WriteLine(accessToken.Token); System.Diagnostics.Debug.WriteLine(accessToken.TokenSecret); twitterService.AuthenticateWith(accessToken.Token, accessToken.TokenSecret); twitterService.VerifyCredentials((user, verifyResponse) => { if (verifyResponse.StatusCode == HttpStatusCode.OK) { Dispatcher.BeginInvoke(() => MessageBox.Show(user.Name)); } else { // Fails here Dispatcher.BeginInvoke(() => MessageBox.Show("Failed to connect to Twitter. Please try again.1\n" + verifyResponse.StatusDescription)); } }); } else { Dispatcher.BeginInvoke(() => MessageBox.Show("Failed to connect to Twitter. Please try again.0\n" + response.StatusDescription)); } }); } }
EDIT 2: Could this be with this? https://dev.twitter.com/blog/ssl-upgrade-for-twitterapi