/// <summary> /// Google Plus user profile event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGPUserProfile_Click(object sender, EventArgs e) { try { //Retrieve value from settings file var googleAPIKeySettings = ConfigurationManager.AppSettings["GoogleAPIKey"]; string googleAPIKey = string.Empty; if (googleAPIKeySettings != null) googleAPIKey = googleAPIKeySettings.ToString(); GoogleClient gpClient = new GoogleClient(); txtGPResponse.Text = gpClient.searchUsers(txtGPRequest.Text,googleAPIKey); } catch (Exception ex) { txtGPResponse.Text = "Error occurred while retrieving google+ user profile! " + ex.Message; } }
/// <summary> /// Retrieves User profile from various social media websites /// </summary> /// <param name="userId"></param> /// <param name="channels"></param> /// <param name="fbAccessToken"></param> /// <returns></returns> public List<Profile> getUserProfile(string userId, string channels, string fbAccessToken) { List<Profile> profiles = new List<Profile>(); if (channels.ToLower().Contains(Channel.Facebook.ToString().ToLower())) { try { FacebookClient fbClient = new FacebookClient(); string fbUser = fbClient.getUserProfile(userId, fbAccessToken); JObject o = JObject.Parse(fbUser); Profile profile = new Profile(); profile.id = (string)o.SelectToken("id"); profile.name = (string)o.SelectToken("name"); profile.firstName = (string)o.SelectToken("first_name"); profile.lastName = (string)o.SelectToken("last_name"); profile.url = (string)o.SelectToken("link"); profile.username = (string)o.SelectToken("username"); profile.location = (string)o.SelectToken("location.name"); profile.gender = (string)o.SelectToken("gender"); profile.locale = (string)o.SelectToken("locale"); profile.updatedTime = (string)o.SelectToken("updated_time"); profile.email = (string)o.SelectToken("email"); profile.channel = Channel.Facebook.ToString(); profiles.Add(profile); } catch { throw; } } if (channels.ToLower().Contains(Channel.Twitter.ToString().ToLower())) { try { TwitterClient twClient = new TwitterClient(); string twUser = twClient.getUserProfile(userId); JObject o = JObject.Parse(twUser); Profile profile = new Profile(); int id = (Int32)o.SelectToken("Id"); profile.id = id.ToString(); profile.name = (string)o.SelectToken("Name"); profile.url = (string)o.SelectToken("Url"); profile.username = (string)o.SelectToken("ScreenName"); profile.location = (string)o.SelectToken("Location"); profile.profileImage = (string)o.SelectToken("ProfileImageUrl"); profile.channel = Channel.Twitter.ToString(); profiles.Add(profile); } catch { throw; } } if (channels.ToLower().Contains(Channel.GooglePlus.ToString().ToLower())) { try { string APIKey = "AIzaSyDfNJfYRM68bj_jRKnD1utAqBaVTrV2WSk"; GoogleClient gpClient = new GoogleClient(); string gpUsers = gpClient.searchUsers(userId, APIKey); JObject joUsers = JObject.Parse(gpUsers); var userList = joUsers["items"].ToList(); foreach (var user in userList) { if (user["id"] != null) { try { string gpUser = gpClient.getUserProfile((string)user["id"], APIKey); JObject o = JObject.Parse(gpUser); Profile profile = new Profile(); profile.id = (string)o.SelectToken("id"); profile.name = (string)o.SelectToken("displayName"); profile.firstName = (string)o.SelectToken("name.givenName"); profile.lastName = (string)o.SelectToken("name.familyName"); profile.gender = (string)o.SelectToken("gender"); profile.url = (string)o.SelectToken("url"); profile.profileImage = (string)o.SelectToken("image.url"); profile.channel = Channel.GooglePlus.ToString(); var placeList = o.SelectToken("placesLived") == null ? new List<JToken>() : o.SelectToken("placesLived").ToList(); foreach (var place in placeList) { if (place.Last.ToString().Contains("primary")) profile.location = place.First.First.ToString(); } profiles.Add(profile); } catch { throw; } } } } catch { throw; } } return profiles; }