Twitter, OAuth, Hammock, TweetSharp, and Windows Phone 7

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

+4
source share
2 answers

I did it! Turns out Twitter was returning the Gzipped access token. Using the method described in the blog post, I had to modify the second RestClient, which will be built as follows:

 var client = new RestClient { Authority = "https://api.twitter.com/oauth", Credentials = credentials, HasElevatedPermissions = true, SilverlightAcceptEncodingHeader = "gzip", DecompressionMethods = DecompressionMethods.GZip }; 

And now it works!

+4
source

I have the same problem, but I did not understand your solution, could you explain a little more where you changed the client-client?

----- EDIT ----

Finally, I was able to get it to work with TweetSharp. I downloaded the source code and added the lines you mentioned to the remaining client configuration that it uses and compiled the project again. Since I cannot click on my changes in this github, I load the dll here. TweetSharp recompiled dll

This is the code I use that works with it

 // Step 1 - Retrieve an OAuth Request Token Service.GetRequestToken((requestToken, response) => { if (response.StatusCode == HttpStatusCode.OK) { Request = requestToken; Uri uri = Service.GetAuthorizationUri(requestToken); Dispatcher.BeginInvoke(() => { Browser.Navigate(uri); } ); } }); //Step 2, get the pincode string html = Browser.SaveToString(); //gets the DOM as a string Regex expression = new Regex(@"<code>(?<word>\w+)</code>"); Match match = expression.Match(html); string pin = match.Groups["word"].Value; if (pin != "") { loginTwitter(pin); //we login with the pin extracted } //step 3, get access tokens from twitter private void loginTwitter(string pin) { Service.GetAccessToken(Request, pin, processAccessToken); } public void processAccessToken(OAuthAccessToken access, TwitterResponse Response){ if (Response.StatusCode == HttpStatusCode.OK) { if (access != null) { Access = access; // Store it for reuse Service.AuthenticateWith(access.Token, access.TokenSecret); } } } 
0
source

Source: https://habr.com/ru/post/1411152/


All Articles