4
        string accessToken = GetAccessToken();
        string accessKey = accessToken.Split('=')[1];

        var client = new FacebookClient(accessKey);
        dynamic me = client.Get("me");

here is the method to get access token and it does return a valid access token

private static string GetAccessToken()
    {
        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=201193246663533&client_secret=secretkeyhere");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "This is a test that posts this string to a Web server.";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;
    }

However, when I debug over

dynamic me = client.Get("me");

throws this exception:

(OAuthException - #2500) An active access token must be used to query information about the current user.

How can i fix this?

DarthVader
  • 52,984
  • 76
  • 209
  • 300

1 Answers1

6

you are getting access token for the APPLICATION, not for a user. Therefore, "me" does not make sense. You should supply ID there - either your user ID, or your app ID, or any other ID your app has permissions for.

both calls worked for with your example:

dynamic me = client.Get("1000<<MY_USER_ID>>5735");

dynamic theApp = client.Get("201193246663533");
avs099
  • 10,937
  • 6
  • 60
  • 110
  • thanks for the answer but how do i get the user per request. I m not interested in my info, i want the users info. therefore if u look at:http://csharpsdk.org/docs/web/getting-started.html , it says i need to use "me" – DarthVader Apr 10 '12 at 21:15
  • 1
    i think you are confused. You cannot get access token for the user without displaying FB authentication dialog. What you are getting with your code is APPLICATION token - which you can use, for example, to get Insight data, or create test users, and so on. If you want to authenticate a USER - then you should use either server-side flow, or Javascript SDK. – avs099 Apr 10 '12 at 21:23
  • 1
    check this out: http://stackoverflow.com/questions/8625708/asp-net-website-authentication-with-facebook/8625873#8625873 - that can help you to understand server-side flow for authentication. And here is the official page for server-side authentication: https://developers.facebook.com/docs/authentication/server-side/ – avs099 Apr 10 '12 at 21:27
  • @avs099 can we get friends info and post without using dialog . if follow server-side flow link you provided but that's looks old now you can check my comments there – Heemanshu Bhalla Aug 23 '16 at 08:42