Foursquare–part 2 of 4
After successfully connecting to Foursquare with an Web application, the problem is what to don with a Desktop application. The most simple desktop application is a Console . The first idea that I have had is to start a browser, let the user pass his credentials and retrieve data from browser.
Something like this
string urlAuth = this.sharpSquare.GetAuthenticateUrl(urlSOA + "home/redirect4sq/" + this.thisRequest); Process p = new Process(); p.StartInfo.FileName = getDefaultBrowser(); p.StartInfo.Arguments = urlAuth; p.Start();
However, I have been not capable of taking data from the WebBrowser process. So the solution is this one:
The Console program open a web browser to the Foursquare web site with a redirect to http://fsq.apphb.com/ and GUID. The user enters his credentials to FourSquare site. Then Foursquare is redirecting to the http://fsq.apphb.com/ -and the http://fsq.apphb.com/ memorizes a GUID and his token. Then the Console reads from http://fsq.apphb.com/ the token ( has the GUID) and it is set.
The code looks like this:
public void AuthenticateNewWebBrowser() { string urlAuth = this.sharpSquare.GetAuthenticateUrl(urlSOA + "home/redirect4sq/" + this.thisRequest); Process p = new Process(); p.StartInfo.FileName = getDefaultBrowser(); p.StartInfo.Arguments = urlAuth; p.Start(); Thread.Sleep(5000); AuthenticateToken(); } //retrieving from website public void AuthenticateToken() { var newUrl = urlSOA + "api/Values/ClientToken/" + this.thisRequest; var code = new WebClient().DownloadString(newUrl).Replace("\"", ""); SetAccessCode(code); }
Next time I will do code for solving same problem – but from inside the program( without needing to launch a new Process)
Leave a Reply