// Post api/FBGetProfile public async Task<HttpResponseMessage> Post(AccountId accountid) { HttpStatusCode httpStatus = HttpStatusCode.OK; MobileServiceContext context = new MobileServiceContext(); ServiceUser user = this.User as ServiceUser; if (user == null) { throw new InvalidOperationException("This can only be called by authenticated clients"); } FacebookCredentials creds = (await user.GetIdentitiesAsync()).OfType<FacebookCredentials>().FirstOrDefault(); if (creds != null) { var fbDataProvider = new FBProfileDataProvider(creds.AccessToken); Network facebook = context.Networks.First(n => n.Id == (Int16)NetworkType.FACEBOOK); ProfileData fbProfileData = await fbDataProvider.GetUserInfo(); UserInfo userInfo; if (context.UserInfos.Any(ui => ui.UserId == accountid.accountid)) { userInfo = context.UserInfos.Include("UserInfoDetail").First(ui => ui.UserId == accountid.accountid); userInfo.Bio = fbProfileData.Bio; userInfo.About = fbProfileData.About; userInfo.ProfilePicUrl = fbProfileData.ProfilePicUrl; // TODO: Update other propoerties UserInfoDetail detail = userInfo.UserInfoDetails.First(x => x.NetworkId == facebook.Id); if (detail == null) { detail = new UserInfoDetail() { JsonInfo = fbProfileData.AsJsonString(), NetworkId = facebook.Id }; userInfo.UserInfoDetails.Add(detail); } else detail.JsonInfo = fbProfileData.AsJsonString(); } else { userInfo = new UserInfo() { UserId = accountid.accountid }; userInfo.SetProfileInfo(fbProfileData); userInfo.Id = Guid.NewGuid().ToString(); userInfo.UserInfoDetails.Add(new UserInfoDetail() { JsonInfo = fbProfileData.AsJsonString() }); context.UserInfos.Add(userInfo); } context.SaveChanges(); httpStatus = HttpStatusCode.Created; return this.Request.CreateResponse(httpStatus, userInfo); } throw new Exception("Facebook identity not found"); }
// Post api/SyncInconsistentProfile public async Task<HttpResponseMessage> Post(SyncInconsistentProfileRequest requestParams) { HttpStatusCode httpStatus = HttpStatusCode.OK; MobileServiceContext context = new MobileServiceContext(); ServiceUser user = this.User as ServiceUser; //Todo first userInfo is Empty if (user == null) { throw new InvalidOperationException("This can only be called by authenticated clients"); } Account account = context.Accounts.FirstOrDefault(a => a.Id == requestParams.accountid); UserInfo userInfo = context.UserInfos.Include("UserInfoDetails").FirstOrDefault(ui => ui.UserId == requestParams.accountid); if (requestParams.provider == 0 || requestParams.provider == 1) { FacebookCredentials creds = (await user.GetIdentitiesAsync()).OfType<FacebookCredentials>().FirstOrDefault(); if (creds != null) { ETLProfileDataProvider dataProvider = new FBProfileDataProvider(creds.AccessToken); ProfileData fbProfileData = await dataProvider.GetUserInfo(); var detail = (userInfo != null) ? userInfo.UserInfoDetails.FirstOrDefault(x => x.NetworkId == (int)NetworkType.FACEBOOK): new UserInfoDetail(); if(detail == null) detail = new UserInfoDetail() { Id = Guid.NewGuid().ToString() }; detail.JsonInfo = fbProfileData.AsJsonString(); detail.SetNetork(context.Networks.First(n => n.Id == (int)NetworkType.FACEBOOK)); if(userInfo == null) { userInfo = new UserInfo(); userInfo.Id = Guid.NewGuid().ToString(); } userInfo.SetProfileInfo(fbProfileData); if(userInfo.UserInfoDetails != null && !userInfo.UserInfoDetails.Any(x => x.NetworkId == (int)NetworkType.FACEBOOK)) userInfo.UserInfoDetails.Add(detail); account.FacebookId = creds.UserId; context.SaveChanges(); httpStatus = HttpStatusCode.Created; } } if(requestParams.provider == 0 || requestParams.provider == 2) { LinkedInCredentials creds = (await user.GetIdentitiesAsync()).OfType<LinkedInCredentials>().FirstOrDefault(); if (creds != null) { ETLProfileDataProvider dataProvider = new LinkedInProfileDataProvider(creds.AccessToken); ProfileData linkedInProfileData = await dataProvider.GetUserInfo(); var detail = (userInfo != null) ? userInfo.UserInfoDetails.FirstOrDefault(x => x.NetworkId == (int)NetworkType.LINKED_IN): new UserInfoDetail(); if (detail == null) detail = new UserInfoDetail() { Id = Guid.NewGuid().ToString() }; detail.JsonInfo = linkedInProfileData.AsJsonString(); detail.SetNetork(context.Networks.First(n => n.Id == (int)NetworkType.LINKED_IN)); if (userInfo == null) { userInfo = new UserInfo(); userInfo.Id = Guid.NewGuid().ToString(); } userInfo.SetProfileInfo(linkedInProfileData); if (userInfo.UserInfoDetails != null && !userInfo.UserInfoDetails.Any(x => x.NetworkId == (int)NetworkType.LINKED_IN)) userInfo.UserInfoDetails.Add(detail); account.LinkedInId = creds.UserId; context.SaveChanges(); httpStatus = HttpStatusCode.Created; } } return this.Request.CreateResponse(httpStatus, userInfo); }