0

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);
}
4

3 に答える 3

2

C# では、ユーザーの介入なしではアクセス トークンを取得できません。pord911 が提供するソリューションがあります。しかし、彼が言ったように、それはまだ厄介です。Foursequare は、Twitter と同じ方法で Oauth2 Rest API を再設計する必要があります。

そうは言っても、この作品を見たいと思うかもしれません: https://codeload.github.com/ignatandrei/4SqDayHistory/zip/master

于 2016-12-25T01:06:13.453 に答える
0

URLのつづりを間違えたようです。foursquare.com/oauth2を読み取る必要があるときに、foursquare.com/oaut2があります。

また、不正な形式のURLがあることに注意してください。「http:localhost:8080」ではなく、「http:// localhost:8080」である必要があります。コードと、 foursquare.com/oauthで登録されているリダイレクトURIに必ず変更を加えてください。

于 2012-11-08T19:53:29.207 に答える
0

返信いただきありがとうございます。しかし、access_token を取得するための初期 URL を使用してボタンのクリックで起動する webbrowser オブジェクトで問題を解決しました。access_token を使用して「http://localhost...」URL にリダイレクトが発生すると、「access_token」を検索してキャッチするハンドラーで Form_Navigated イベントが発生します。「access_token」が見つかると、webbrowser ウィンドウが自動的に非表示になり、最初のウィンドウと foursquare からのリストされたデータが残ります。access_token を取得するためだけに空白のフォームを数秒間表示するのはちょっと厄介に見えますが、機能します。このソリューションはフォースクエアで見つけました。しかし、彼らは Android アプリのソリューションを提示しました。ここにリンクがあります。

https://github.com/foursquare/android-oauth-example/blob/master/workspace/android-oauth-example/src/com/foursquare/android/oauth/ActivityWebView.java

于 2012-11-15T21:30:33.853 に答える