locked
Publish on a user's facebook wall from my web application RRS feed

  • Question

  • User838769528 posted

    Hi,

    I'm tryng to publish something in a wall of a certain user.

    I'm using the sdk from codeplex

    I reached how to get permissions from the user (offline_access, publish stream) The problem is that I don't know how to get the Session key: this is my piece of code when I return from the facebook Permision page.

    I have a web page that redirects to facebook

     

     

    string uri = string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope=user_about_me,email,publish_stream,offline_access&return_session=true",
                        applicationKey, redirectUrl);
    
    
    

     

    but the session doesn`t comes when I try to get it with a Request.Form["session"]

    this is my piece of code:

     

     

    string ApplicationKey = ApplicationSettings.FacebookApplicationKey;
            string SecretKey = ApplicationSettings.FacebookSecretKey;
            string redirectUrl = ApplicationSettings.FacebookUrlAfterConnectFromFacebookCampaign;
            redirectUrl = HttpUtility.UrlEncode(string.Format(redirectUrl + "{0}", "&connect=1"));
    
            if (Request.QueryString["code"] != null)
            {
                string accessTokenUrl = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}",
                    ApplicationKey, redirectUrl, SecretKey, Request.QueryString["code"]);
    
                HttpWebRequest request = WebRequest.Create(accessTokenUrl) as HttpWebRequest;
                request.Method = "GET";
                WebResponse webResponse = request.GetResponse();
                Stream stream = webResponse.GetResponseStream();
                StreamReader str = new StreamReader(stream);
                string response = str.ReadToEnd();
    
                NameValueCollection qs = HttpUtility.ParseQueryString(response);
    
                if (qs["access_token"] != null)
                {
                    HttpRuntime.Cache.Insert("access_token", qs["access_token"], null, DateTime.Now.AddMinutes(Convert.ToDouble(qs["expires"])), TimeSpan.Zero);
                   // string token = HttpRuntime.Cache["access_token"].ToString();
                    Facebook.FacebookAPI api = new Facebook.FacebookAPI(qs["access_token"]);
                    JSONObject me = api.Get("/me");
    
                    user.EMail = me.Dictionary["email"].String;
                    user.Password = string.Empty;
                    if (me.Dictionary.ContainsKey("first_name"))
                    {
                        user.FirstName = me.Dictionary["first_name"].String;
                    }
                    if (me.Dictionary.ContainsKey("last_name"))
                    {
                        user.LastName = me.Dictionary["last_name"].String;
                    }
                    if (me.Dictionary.ContainsKey("gender"))
                    {
                        user.Gender = me.Dictionary["gender"].String.ToLower().Equals("male") ? true : false;
                    }
                    user.DateOfBirth = DateTime.Now;
    
                    string pictureUrl = string.Format("https://graph.facebook.com/{0}/picture?type=large", me.Dictionary["id"].Integer);
                    HttpWebRequest pictureRequest = WebRequest.Create(pictureUrl) as HttpWebRequest;
                    request.Method = "GET";
                    WebResponse pictureWebResponse = pictureRequest.GetResponse();
                    user.PictureStream = pictureWebResponse.GetResponseStream();
    
                    facebook.API apisss = new API();
    
                    apisss.ApplicationKey = ApplicationKey;
                    apisss.Secret = SecretKey;
                    apisss.uid = (long)me.Dictionary["id"].Integer;
                    apisss.AuthToken = qs["access_token"];                   
    
                    apisss.stream.publish("hola");
    
    
                }
                user.UsesFConnect = true;
                user.RegisterId = (int)RegisterType.Fb_Campaign;
    
            }
            return user;
    
    
    

    but when I try to publish an exception is thrown that I need the session key. So my question is how can I get the session key???

    Thanx!!

     

    Saturday, April 30, 2011 12:19 PM

All replies

  • User1682618242 posted

    I think the session should be obtained from the request (HttpWebRequest request).

    I use the javascript SDK for the same thing. See bellow:

     

    function postToFacebookWall() {
        FB.ui(
        {
            method: 'stream.publish',
            message: "...",
            attachment: {
                name: "...",
                caption: '...',
                description: (
              "..."
            ),
                href: 'http://www.....'
            },
            action_links: [
            { text: ...', href: 'http://www.... }
          ],
            user_message_prompt: "...."
        }
      );
    }

    You need to reference:

    <script type="text/javascript" charset="utf-8" src="http://connect.facebook.net/en_US/all.js"></script>

    and to init:

    FB.init({ appId: '....', status: true, cookie: true, xfbml: true });
    Saturday, April 30, 2011 12:54 PM
  • User1097967958 posted

    I am also getting access token from request code. Can u help me on this?

    Wednesday, September 26, 2012 5:35 AM