Ejemplo n.º 1
0
        public static FacebookOAuthResult <FacebookUser> GetUserProfileSync(FacebookSignedRequest signedRequest, string fields)
        {
            if (signedRequest == null)
            {
                Logger.Current.Error("GetUserProfileSync", "The Facebook signed request is null. Cannot request the facebook profile");
                return(null);
            }

            FacebookOAuthResult <FacebookUser> result = new FacebookOAuthResult <FacebookUser>();

            if (string.IsNullOrEmpty(fields))
            {
                fields = "likes.offset(0).limit(1000),id,name,first_name,last_name,gender,locale,picture,hometown,link,birthday,location,meeting_for,email,timezone,relationship_status,significant_other";
            }

            IStopwatch watch = MonitoringTimers.Current.GetNewStopwatch(true);

            try
            {
                dynamic d = GetUserProfileFieldSync(signedRequest.UserID, fields, signedRequest.AccessToken);

                if ((bool)d.CredentialsHaveExpired)
                {
                    result.SetHasExpired();
                }
                result.Data = ConvertToFacebookUser(d);
            }
            catch (FacebookOAuthException)
            {
                result.SetHasExpired();
            }
            catch (Exception e)
            {
                Logger.Current.Error("Profile.GetUserProfileSync", "Could not retrieve Facebook user's profile information", e);
            }
            finally
            {
                watch.Stop();
                var counter = Counters.Facebook_GetUserProfile;
                if (fields.Contains("likes"))
                {
                    counter = Counters.Facebook_GetUserLikesAndProfile;
                }
                MonitoringTimers.Current.AddTime(counter, watch);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static FacebookOAuthResult <FacebookLikes> GetAllUserLikes(string facebookId, string accessToken)
        {
            FacebookOAuthResult <FacebookLikes> result = new FacebookOAuthResult <FacebookLikes>();

            result.Data = new FacebookLikes();
            IStopwatch watch = MonitoringTimers.Current.GetNewStopwatch(true);

            try
            {
                result.Data.Next = facebookId + "/likes?format=json&limit=5000";
                do
                {
                    dynamic d = GraphAPI.Get(result.Data.Next, accessToken);

                    FacebookLikes pageResult = ConvertToFacebookLikes(d);

                    if (pageResult.Likes != null && pageResult.Likes.Count > 0)
                    {
                        result.Data.Likes.AddRange(pageResult.Likes);
                        result.Data.Next = pageResult.Next;

                        if (pageResult.Likes.Count < 5000)
                        {
                            result.Data.Next = null;
                        }
                    }
                    else
                    {
                        result.Data.Next = null;
                    }
                }while (!string.IsNullOrEmpty(result.Data.Next));
            }
            catch (FacebookOAuthException)
            {
                result.SetHasExpired();
            }
            catch (Exception e)
            {
                result.HasError = true;
                Logger.Current.Error("SocialGraph.GetFriendIds", "Could not update a Facebook user's social graph", e);
            }
            finally
            {
                //TODO : ajouter un compteur sur le nombre de like récupérés
                watch.Stop();
                MonitoringTimers.Current.AddTime(Counters.Facebook_GetUserLikes, watch);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Renew a user's Facebook access_token using the "code" parameter passed by Facebook
        /// </summary>
        public static FacebookOAuthResult <string> ExchangeCodeForAccessToken(string applicationId, string secretKey, string code, out DateTime expires)
        {
            FacebookOAuthResult <string> result = new FacebookOAuthResult <string>();

            expires = DateTime.MinValue;

            string url = string.Format(RenewAccessTokenURL,
                                       applicationId,
                                       secretKey,
                                       code);

            try
            {
                Logger.Current.Debug("ExchangeCodeForAccessToken", "Getting new access_token", url);

                string s = GraphAPI.GetRaw(url);
                if (s.StartsWith("access_token="))
                {
                    string[] split            = s.Split('&');
                    string   accessToken      = split[0].Substring(split[0].IndexOf('=') + 1);
                    double   expiresInSeconds = double.Parse(split[1].Substring(split[1].IndexOf('=') + 1));
                    expires = DateTime.UtcNow.AddSeconds(expiresInSeconds);

                    result.Data = accessToken;
                }
            }
            catch (FacebookOAuthException fbe)
            {
                Logger.Current.Error("ExchangeCodeForAccessToken", "Error getting new access_token", fbe, url);

                result.SetHasExpired();
            }
            catch (Exception e)
            {
                Logger.Current.Error("Authentication.RenewAccessToken", "Could not renew the user's access_token", e);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static FacebookOAuthResult <dynamic> GetUserProfileFieldSync(string facebookId, string fields, string accessToken)
        {
            FacebookOAuthResult <dynamic> result = new FacebookOAuthResult <dynamic>();

            IStopwatch watch = MonitoringTimers.Current.GetNewStopwatch(true);

            try
            {
                dynamic d = GraphAPI.Get(
                    string.Format("{0}?fields={1}",
                                  facebookId,
                                  fields), accessToken);

                result.Data = d;
            }
            catch (FacebookOAuthException)
            {
                result.SetHasExpired();
            }
            catch (Exception e)
            {
                Logger.Current.Error("Profile.GetUserProfileFieldSync", "Could not retrieve Facebook user's profile field", e, fields);
            }
            finally
            {
                watch.Stop();
                var counter = Counters.Facebook_GetUserProfile;
                if (fields.Contains("likes"))
                {
                    counter = Counters.Facebook_GetUserLikesAndProfile;
                }
                MonitoringTimers.Current.AddTime(counter, watch);
            }

            return(result);
        }