Esempio n. 1
0
        private void btnGetMyInfo_Click(object sender, RoutedEventArgs e)
        {
            var fb = new Facebook(txtAccessToken.Text);

            // using async method, silverlight supports only async methods
            fb.GetAsync("/me",
                result =>
                {
                    // incase you are using async,
                    // always check if it was successful.
                    if (result.IsSuccessful)
                    {
                        // this prints out the raw json, // make sure u write on the appropriate thread
                        WriteLine(result.RawResponse + "\n");

                        // this mite be preferable - the generic version of the result
                        var user = result.GetResponseAs<User>();
                        WriteLine("Hi " + user.Name);
                    }
                    else
                    {
                        // exception is stored in result.Exception
                        // u can extract the message using result.Exception.Message
                        // or u can get raw facebook json exception using result.Response.
                        WriteLine("Error: " + "\n" + result.Exception.Message + "\n");
                    }
                });

            //// example for posting on the wall:
            //fb.PostAsync("/me/feed",
            //             new Dictionary<string, string>
            //                 {
            //                     { "message", "testing form Silverlight FacebookSharp" }
            //                 },
            //             result =>
            //             {
            //                 if (result.IsSuccessful)
            //                 {
            //                     WriteLine("New post id" + result.RawResponse);
            //                 }
            //                 else
            //                 {
            //                     WriteLine("Error: " + "\n" + result.Exception.Message + "\n");
            //                 }
            //             });

            //// example for deleting
            //fb.DeleteAsync("/id",
            //               result =>
            //               {
            //                   if (result.IsSuccessful)
            //                   {
            //                       WriteLine(result.RawResponse);
            //                   }
            //                   else
            //                   {
            //                       WriteLine("Error: \n" + result.Exception.Message);
            //                   }
            //               });
        }
Esempio n. 2
0
        public static FacebookAuthenticationResult Parse(string url, FacebookSettings facebookSettings)
        {
            IDictionary <string, string> paramters;

            if (url.StartsWith("http://www.facebook.com/connect/login_success.html"))
            {
                Uri uri = new Uri(url);
                if (!string.IsNullOrEmpty(uri.Fragment))
                {
                    var pars = FacebookUtils.ParseUrlQueryString(uri.Fragment);
                    paramters = new Dictionary <string, string>();
                    foreach (var p in pars)
                    {
                        if (p.Key.StartsWith("#"))
                        {
                            paramters.Add(p.Key.Substring(1), p.Value[0]);
                        }
                        else
                        {
                            paramters.Add(p.Key, p.Value[0]);
                        }
                    }
                }
                else
                {
                    var pars = FacebookUtils.ParseUrlQueryString(url);
                    paramters = new Dictionary <string, string>();
                    foreach (var p in pars)
                    {
                        paramters.Add(p.Key, p.Value[0]);
                    }
                }

                return(new FacebookAuthenticationResult(paramters));
            }
            // for now don't allow to parse silverlight and windows phone web,
            // coz ExchangeAccessTokenForCode needs to have async version.
#if !(SILVERLIGHT || WINDOWS_PHONE)
            else
            {
                // its from web
                var uri  = new Uri(url);
                var pars = FacebookUtils.ParseUrlQueryString(uri.Query);

                paramters = new Dictionary <string, string>();
                foreach (var p in pars)
                {
                    paramters.Add(p.Key, p.Value[0]);
                }

                if (paramters.ContainsKey("signed_request"))
                {   // if we are accessing from iframe canvas
                    // note: needs to enable Canvas Session Parameter and OAuth 2.0 for Canvas (beta) in Migration Tab in app settings.
                    // might add other features later on.

                    if (facebookSettings == null)
                    {
                        throw new ArgumentNullException("facebookSettings");
                    }
                    if (string.IsNullOrEmpty(facebookSettings.ApplicationSecret))
                    {
                        throw new ArgumentNullException("facebookSettings.ApplicationSecret");
                    }

                    IDictionary <string, object> jsonObject;
                    if (!ValidateSignedRequest(paramters["signed_request"], facebookSettings.ApplicationSecret, out jsonObject))
                    {
                        throw new InvalidSignedRequestException();
                    }

                    return(new FacebookAuthenticationResult(jsonObject));
                }
                else if (paramters.ContainsKey("code"))
                {   // incase this is from the web, we need to exchange the code with access token
                    if (facebookSettings == null)
                    {
                        throw new ArgumentNullException("facebookSettings");
                    }

                    long   expiresIn;
                    string accessToken = Facebook.ExchangeAccessTokenForCode(paramters["code"],
                                                                             facebookSettings.ApplicationKey,
                                                                             facebookSettings.ApplicationSecret,
                                                                             facebookSettings.PostAuthorizeUrl,
                                                                             facebookSettings.UserAgent,
                                                                             out expiresIn);
                    return(new FacebookAuthenticationResult(accessToken, expiresIn, null));
                }
            }
#endif
            // if its parse error
            return(new FacebookAuthenticationResult(string.Empty, 0, string.Empty));
        }
Esempio n. 3
0
 public FacebookApiRestSharpMessage(Facebook fb)
     : base(fb)
 {
     BaseUrl = ApiBaseUrl;
 }