Reputation: 111
I have a problem with obtaining access token from FourSquare for my C# desktop app. I registered my app at their site and obtained the client id and secret id. For my callback url I put http..localhost:8080. I didn't know what to put, I know it doesn't make much sense but there it is.
I found a way to obtain access token with my browser. When I try the following url:
https://foursquare.com/oauth2/authenticate?client_id=MY_CLIENT_ID&response_type=token&redirect_uri=https:localhost:8080/
it tries to redirect to:
http:localhost:8080/?accsess_token=OBTAINED_ACCSESS_TOKEN
So there you go, I got my access token. Now, I tried to do some similar thing in my C# code by watching the response headers from my C#, and in the Location header there should be:
http:localhost:8080/?accsess_token=OBTAINED_ACCSESS_TOKEN
But I have a problem there is no Location header in the response. When I watch the response header in HttpFox in my browser there is a Location header with my link inside the response.
I have posted here what I have done so far, and I am hitting a brick wall here. If anyone knows any other way to obtain an access token for a desktop app from FourSquare, or has a solution for the problem with headers above. Please post it. If anyone has an answer for this I'll buy him a beer because this is a part of my assignment for a job. :)
Here is my code so far.
string url = "https://foursquare.com/oaut2/authenticate?client_id=E4HFYP1LRDSAL21WJVJ1EBT1NSG1DPRHSNXN0PFI10UIOX0N&response_type=token&redirect_uri=https:localhost:8080/";
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = WebRequest.Create(url) as HttpWebRequest;
response = request.GetResponse() as HttpWebResponse;
request.AllowAutoRedirect = false;
int status = (int)response.StatusCode;
Console.WriteLine("Request headers: ");
Console.WriteLine("-------------------------------------------------------------");
foreach (string s in request.Headers)
{
Console.WriteLine(s + ": " + request.Headers[s]);
}
Console.WriteLine("Response headers: ");
Console.WriteLine("-------------------------------------------------------------");
foreach (string header in response.Headers)
Console.WriteLine(header + ": " + response.Headers[header]);
}
catch (Exception x)
{
Console.WriteLine(x.Message);
}
Upvotes: 0
Views: 661
Reputation: 316
In C# you can't get access token without user intervention, there is the solution offered by pord911. But as he said, its still nasty. Foursequare should redesign their Oauth2 Rest API in the same way as twitter's.
This being said, you may want to look at this work : https://codeload.github.com/ignatandrei/4SqDayHistory/zip/master
Upvotes: 2
Reputation: 111
Thanks for the reply. But I solved the problem with a webbrowser object which fires on a button click with the initial url for getting the access_token. When redirect is fired to my "http://localhost..." url with the access_token a Form_Navigated event is fired with a handler which searches for 'access_token' and catches it. When the 'access_token' is found the webbrowser window automatically hides, leaving you with your initial window and listed data from foursquare. It looks kinda nasty showing a blank form for a few seconds just to get the access_token but it works. I found this solution on foursquare. But they presented a solution for Android app. Here is the link.
Upvotes: 0
Reputation: 3927
Looks like you misspelled the url. You have foursquare.com/oaut2, when it should read foursquare.com/oauth2
Also, note that you have a malformed URL. It should be "http://localhost:8080", and not "http:localhost:8080". Be sure to make that change in your code as well as in your registered redirect URI at foursquare.com/oauth
Upvotes: 0