Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }