private static JSONObject GetUserDetails(string UserID, string oAuthToken) { JSONObject obj = null; string url; HttpWebRequest request; try { url = string.Format("https://graph.facebook.com/{0}?access_token={1}", UserID, oAuthToken); //now send the request to facebook request = WebRequest.Create(url) as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string retVal = reader.ReadToEnd(); obj = JSONObject.CreateFromString(retVal); if (obj.IsDictionary && obj.Dictionary.ContainsKey("error")) { throw new Exception(obj.Dictionary["error"].Dictionary["type"].String, new Exception(obj.Dictionary["error"].Dictionary["message"].String)); } } } catch (Exception Ex) { throw Ex; //LblMessage = new Label(); //LblMessage.Text = Ex.Message; //this.Controls.Add(LblMessage); } return(obj); }
private JSONObject GetUserGroups(string oAuthToken) { JSONObject obj = null; string url; HttpWebRequest request; try { url = string.Format("https://graph.facebook.com/me/groups?access_token={1}", this.OAuthPageID.Trim().ToString(), oAuthToken); request = WebRequest.Create(url) as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string retVal = reader.ReadToEnd(); obj = JSONObject.CreateFromString(retVal); if (obj.IsDictionary && obj.Dictionary.ContainsKey("error")) { throw new Exception(obj.Dictionary["error"].Dictionary["type"].String, new Exception(obj.Dictionary["error"].Dictionary["message"].String)); } } } catch (Exception Ex) { LblMessage = new Label(); LblMessage.Text = Ex.Message; this.Controls.Add(LblMessage); } return(obj); }
private JSONObject GetFeeds(string FeedURL) { JSONObject obj = null; string url; HttpWebRequest request; try { if (string.IsNullOrEmpty(FeedURL)) { if (!String.IsNullOrEmpty(oAuthToken)) { if (IsPosts) { //if we need to show the user feeds only then call posts rest api url = string.Format("https://graph.facebook.com/{0}/posts?access_token={1}&limit={2}", this.UserID, oAuthToken, WallCount); } else { //else we need to call the feed rest api url = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&limit={2}", this.UserID, oAuthToken, WallCount); } } else { throw (new Exception("The access token returned was not valid.")); } } else { //this is the url that we got for next feed url...no need to generate the url from scratch url = FeedURL; } //now send the request to facebook request = WebRequest.Create(url) as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string retVal = reader.ReadToEnd(); obj = JSONObject.CreateFromString(retVal); if (obj.IsDictionary && obj.Dictionary.ContainsKey("error")) { throw new Exception(obj.Dictionary["error"].Dictionary["type"].String, new Exception(obj.Dictionary["error"].Dictionary["message"].String)); } } } catch (Exception Ex) { LblMessage = new Label(); LblMessage.Text = Ex.Message; this.Controls.Add(LblMessage); } return(obj); }
/// <summary> /// Get feeds from Facebook /// </summary> /// <param name="FeedURL"></param> /// <returns></returns> private JSONObject GetFeeds(string FeedURL) { JSONObject obj = null; //Check for the JSON object in the cache and check for the feedurl to identify if the request is for the Older posts if (HttpContext.Current.Cache.Get(CacheJSONKey) != null && string.IsNullOrEmpty(FeedURL)) { //If the WebPart is edited then clear the cache if (this.WebPartManager.DisplayMode == WebPartManager.EditDisplayMode) { HttpContext.Current.Cache.Remove(CacheJSONKey); } else { obj = HttpContext.Current.Cache.Get(CacheJSONKey) as JSONObject; } } if (obj == null) { //Call for the OAuth method to get the feeds only if the request is for the first time ( not for the "Older Post") if (string.IsNullOrEmpty(FeedURL)) { if (string.IsNullOrEmpty(this.AuthToken)) { oAuthToken = CommonHelper.GetOAuthToken("read_stream", OAuthClientID, OAuthRedirectUrl, OAuthClientSecret, OAuthCode); this.AuthToken = oAuthToken; this.SetPersonalizationDirty(); } else { oAuthToken = this.AuthToken; } } string url; HttpWebRequest request; try { if (string.IsNullOrEmpty(FeedURL)) { if (!String.IsNullOrEmpty(oAuthToken)) { if (IsPosts) { //if we need to show the user feeds only then call posts rest api url = string.Format("https://graph.facebook.com/{0}/posts?access_token={1}&limit={2}", this.UserID, oAuthToken, WallCount); } else { //else we need to call the feed rest api url = string.Format("https://graph.facebook.com/{0}/feed?access_token={1}&limit={2}", this.UserID, oAuthToken, WallCount); } } else { throw (new Exception("The access token returned was not valid.")); } } else { //this is the url that we got for next feed url...no need to generate the url from scratch url = FeedURL; } //now send the request to facebook request = WebRequest.Create(url) as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string retVal = reader.ReadToEnd(); obj = JSONObject.CreateFromString(retVal); //Cache only first page posts (No older posts) if (string.IsNullOrEmpty(FeedURL)) { HttpContext.Current.Cache.Add(CacheJSONKey, obj, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null); } if (obj.IsDictionary && obj.Dictionary.ContainsKey("error")) { throw new Exception(obj.Dictionary["error"].Dictionary["type"].String, new Exception(obj.Dictionary["error"].Dictionary["message"].String)); } } } catch (Exception Ex) { LblMessage = new Label(); LblMessage.Text = Ex.Message; this.Controls.Add(LblMessage); } } return(obj); }