internal static void clearSocialSessions(Action <Boolean, Exception> callback) { NetmeraUser.logout(); NetmeraTwitterUtils.clearTwitterSession((logout, ex) => { if (logout && ex == null) { NetmeraFacebookUtils.clearFacebookSession((logout1, ex1) => { if (logout1 && ex1 == null) { callback(logout1, ex1); } else { if (callback != null) { callback(false, ex1); } } }); } else { if (callback != null) { callback(false, ex); } } }); }
/// <summary> /// Logs a user into the registered application. Email and password fields of /// user is used for this operation. /// </summary> /// <param name="email">Email of the user</param> /// <param name="password">Password of the user</param> /// <returns>The logged user</returns> public static NetmeraUser login(String email, String password) { NetmeraUser user = new NetmeraUser(); try { RequestItem loginUserReqItem = new RequestItem(); loginUserReqItem.setEmail(email); loginUserReqItem.setPassword(password); JObject jLogin = login(loginUserReqItem); if (jLogin != null) { user = setCurrentUser(jLogin); } else { throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Response of user login method is null"); } } catch (WebException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Web exception occurred in user login method"); } catch (IOException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in user login method"); } catch (JsonException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of user login method is invalid"); } return(user); }
/// <summary> /// Logs a guest into the application. /// </summary> /// <returns>The logged user</returns> public static NetmeraUser loginAsGuest() { NetmeraUser user = new NetmeraUser(); try { JObject jLogin = loginAsGuestRequest(); if (jLogin != null) { user = setCurrentUser(jLogin); } else { throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Response of user login method is null"); } } catch (WebException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Web exception occurred in user login method"); } catch (IOException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in user login method"); } catch (JsonException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of user login method is invalid"); } return(user); }
/// <summary> /// Logs a guest into the application. /// </summary> /// <param name="callback">Method called when user login operation finishes</param> public static void loginAsGuest(Action <NetmeraUser, Exception> callback) { NetmeraUser user = new NetmeraUser(); try { loginAsGuestRequest((json, ex) => { if (json == null || ex != null) { if (callback != null) { callback(null, ex); } } else { try { user = setCurrentUser(json); if (callback != null) { callback(user, ex); } } catch (NetmeraException e) { if (callback != null) { callback(null, e); } } } }); } catch (WebException) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Web exception occurred in user login method")); } } catch (IOException) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in user login method")); } } catch (JsonException) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of user login method is invalid")); } } }
/// <summary> /// /// </summary> /// <param name="jsonArray"></param> /// <returns></returns> protected internal List <NetmeraUser> convertJsonArrayToNetmeraUser(JArray jsonArray) { List <NetmeraUser> userList = new List <NetmeraUser>(); if (jsonArray == null) { throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Json contains user information is null"); } JArray entries = new JArray(); //If the entry is type of JArray, it is already able to be casted to JArray. //Else it does not accept JArray cast. Instead, it is added to a JArray as a JObject if (jsonArray[0]["entry"].Type == JTokenType.Array) { entries = (JArray)jsonArray[0]["entry"]; } else { entries.Add(jsonArray[0]["entry"]); } for (int i = 0; i < entries.Count; i++) { try { JObject json = (JObject)entries[i]; String email = null; if (json[NetmeraConstants.Netmera_UserEmails] != null) { JObject emailObj = (JObject)json[NetmeraConstants.Netmera_UserEmails].First(); if (emailObj[NetmeraConstants.Netmera_UserEmailValue] != null) { email = emailObj[NetmeraConstants.Netmera_UserEmailValue].ToString(); } } if (email != "*****@*****.**") { NetmeraUser user = new NetmeraUser(); user.setUser(json); userList.Add(user); } } catch (JsonException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json contains user information is invalid"); } } return(userList); }
/// <summary> /// Sets the owner to the content. /// </summary> /// <param name="user">Current logged user</param> public void setOwner(NetmeraUser user) { if (user != null) { if (user.GetType() == typeof(NetmeraUser)) { NetmeraClient.setLoggedUserSecurityToken(NetmeraUser.securityToken); } else { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_DATA_TYPE, "You should set the object with the type of NetmeraUser."); } } else { throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "User cannot be null.You should set current logged user."); } }
private static void registerAndLogin(String fbId, String nickname, String name, String surname, String email, Action <NetmeraUser, Exception> callback) { NetmeraUser user = new NetmeraUser(); RequestItem loginFbUserUserReqItem = new RequestItem(); loginFbUserUserReqItem.setFbId(fbId); loginFbUserUserReqItem.setNickname(nickname); loginFbUserUserReqItem.setName(name); loginFbUserUserReqItem.setSurname(surname); loginFbUserUserReqItem.setEmail(email); NetmeraUser.facebookRegister(loginFbUserUserReqItem, (json, ex) => { if (json == null || ex != null) { if (callback != null) { callback(null, ex); } } else { try { user = NetmeraUser.setCurrentUser(json); if (callback != null) { callback(user, ex); } } catch (NetmeraException e) { if (callback != null) { callback(null, e); } } } }); }
internal static NetmeraUser setCurrentUser(JObject json) { NetmeraUser user = new NetmeraUser(); if (json == null) { throw new NetmeraException(NetmeraException.ErrorCode.EC_NULL_EXCEPTION, "Json contains current user info is null"); } try { if (json[NetmeraConstants.SecurityToken_Params] != null) { securityToken = json[NetmeraConstants.SecurityToken_Params].ToString(); } if (json[NetmeraConstants.Netmera_UserEmail] != null) { user.email = json[NetmeraConstants.Netmera_UserEmail].ToString(); } if (json[NetmeraConstants.Netmera_UserNickname] != null) { user.nickname = json[NetmeraConstants.Netmera_UserNickname].ToString(); } if (json[NetmeraConstants.Netmera_UserName] != null) { user.name = json[NetmeraConstants.Netmera_UserName].ToString(); } if (json[NetmeraConstants.Netmera_UserSurname] != null) { user.surname = json[NetmeraConstants.Netmera_UserSurname].ToString(); } currentUser = user; } catch (JsonException) { throw new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_EMAIL, "Json contains current user info is invalid"); } return(user); }
internal static void clearSocialSessions() { NetmeraUser.logout(); NetmeraTwitterUtils.clearTwitterSession(null); NetmeraFacebookUtils.clearFacebookSession(null); }
/// <summary> /// User logged out from the application /// </summary> public static void logout() { securityToken = null; currentUser = null; }
/// <summary> /// Logs a user into the registered application. Email and password fields of /// user is used for this operation. /// </summary> /// <param name="email">Email of the user</param> /// <param name="password">Password of the user</param> /// <param name="callback">Method called when user login operation finishes</param> public static void login(String email, String password, Action <NetmeraUser, Exception> callback) { clearSocialSessions((cleared, exception) => { if (exception != null) { if (callback != null) { callback(null, exception); } } else if (cleared) { NetmeraUser user = new NetmeraUser(); try { RequestItem loginUserReqItem = new RequestItem(); loginUserReqItem.setEmail(email); loginUserReqItem.setPassword(password); login(loginUserReqItem, (json, ex) => { if (json == null || ex != null) { if (callback != null) { callback(null, ex); } } else { try { user = setCurrentUser(json); if (callback != null) { callback(user, ex); } } catch (NetmeraException e) { if (callback != null) { callback(null, e); } } } }); } catch (WebException) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_REQUEST, "Web exception occurred in user login method")); } } catch (IOException) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_IO_EXCEPTION, "IO Exception occurred in user login method")); } } catch (JsonException) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_INVALID_JSON, "Json in the response of user login method is invalid")); } } } else { if (callback != null) { callback(null, exception); } } }); }
/// <summary> /// Login with Twitter account /// </summary> /// <param name="callback">Method to be called when login operation finishes</param> public static void login(Action <NetmeraUser, Exception> callback) { String securityToken = NetmeraClient.getSecurityToken(); if (securityToken != null && securityToken.Trim() != "") { Popup popup = new Popup(); LoginUserControl control = new LoginUserControl(); control.webBrowser.Source = null; control.btnCancel.Click += new RoutedEventHandler((s, e) => { if (callback != null) { callback(null, null); } }); try { NetmeraUser.clearSocialSessions((logout, ex) => { if (logout && ex == null) { control.webBrowser.Loaded += new RoutedEventHandler((s, e) => { twitter.getTwitterLoginUri((loginUri, ex1) => { if (loginUri != null && ex1 == null) { Deployment.Current.Dispatcher.BeginInvoke(() => { control.webBrowser.Navigate(loginUri); }); } else { if (callback != null) { callback(null, ex1); } } }); }); control.webBrowser.LoadCompleted += new LoadCompletedEventHandler((s, e) => { if (e.Uri.AbsoluteUri == Twitter.AuthorizeUrl) { String pinHtml = control.webBrowser.SaveToString(); twitter.getTwitterPin(pinHtml, (pin, ex1) => { if (!String.IsNullOrEmpty(pin)) { twitter.getTwitterAccessToken(e.Uri, (accessToken, ex2) => { Deployment.Current.Dispatcher.BeginInvoke(() => { popup.IsOpen = false; if (ex2 != null) { if (callback != null) { callback(null, ex2); } } else { if (!String.IsNullOrEmpty(accessToken)) { getTwitterUser((user, exc) => { if (user != null && exc == null) { TwitterSession.save(twitter); } callback(user, exc); }); } else { login(callback); } } }); }); } }); } }); Deployment.Current.Dispatcher.BeginInvoke(() => { popup.Child = control; popup.IsOpen = true; }); } else { if (callback != null) { callback(null, ex); } } }); } catch (Exception e) { if (callback != null) { callback(null, e); } } } else { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).")); } } }
/// <summary> /// Twitter user logged out from the application. /// </summary> /// <param name="callback">Method to be called when logout operation finishes, which has a boolean parameter showing logout is successful or not </param> public static void logout(Action <Boolean, Exception> callback) { NetmeraUser.logout(); clearTwitterSession(callback); }
/// <summary> /// Handles rich push. That is, it gives you a WebBrowser object filled with rich push HTML content. /// </summary> /// <param name="callback"></param> public static void handleRichPush(Action <WebBrowser, Exception> callback) { //richPush = true; //richPushId = "5113c5e4e4b0533d1f2d5374"; if (richPush && richPushId != null) { JObject userJson = new JObject(); NetmeraUser user = NetmeraUser.getCurrentUser(); if (user != null) { userJson.Add("name", user.getName()); userJson.Add("surname", user.getSurname()); } getRichPushContent((pushContent, ex) => { resetRichPush(); if (ex != null) { if (callback != null) { callback(null, ex); } } else { if (pushContent.Value <String>("message") == "OK") { pushContent = pushContent.Value <JObject>("result"); String pushData = pushContent.Value <String>(NetmeraConstants.Netmera_Rich_Push_Html); JObject clientJson = pushContent.Value <JObject>(NetmeraConstants.Netmera_Rich_Push_Client_Json); String userString = userJson.ToString(); String clientString = clientJson.ToString(); Deployment.Current.Dispatcher.BeginInvoke(() => { WebBrowser webBrowser = new WebBrowser(); webBrowser.IsScriptEnabled = true; webBrowser.LoadCompleted += new LoadCompletedEventHandler((s, e) => { Deployment.Current.Dispatcher.BeginInvoke(() => { String html = webBrowser.SaveToString(); clientString = Regex.Replace(clientString, "\r", ""); clientString = Regex.Replace(clientString, "\n", ""); userString = Regex.Replace(userString, "\r", ""); userString = Regex.Replace(userString, "\n", ""); String jscript = "var customJson='" + clientString + "'; var currentUser='******'; customJson=JSON.parse(customJson); currentUser=JSON.parse(currentUser); getClientParams(customJson,currentUser);"; try { webBrowser.InvokeScript("eval", jscript); if (callback != null) { callback(webBrowser, null); } } catch (Exception) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_RICH_PUSH_JAVASCRIPT_ERROR, "Javascript error occured. Make sure that you put the proper Javascript code in your push message.")); } } }); }); webBrowser.NavigateToString(pushData); }); } else { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_RICH_PUSH_CONTENT_ERROR, "Rich push -HTML- content is not retrieved properly.")); } } } }); } else { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_RICH_PUSH_ID_EMPTY, "Rich push id is empty. Please check rich push data with the function 'NetmeraPushService.checkRichPush()..' and then 'NetmeraPushService.isRichPush()..")); } } }
/// <summary> /// Logs a user into the registered application with Facebook account with Facebook permissions. /// </summary> /// <param name="permissionArray">Permission array</param> /// <param name="callback">Method to be called when login operation finishes</param> public static void login(String[] permissionArray, Action <NetmeraUser, Exception> callback) { String securityToken = NetmeraClient.getSecurityToken(); if (securityToken != null && securityToken.Trim() != "") { Popup popup = new Popup(); LoginUserControl control = new LoginUserControl(); control.btnCancel.Click += new RoutedEventHandler((s, e) => { NetmeraUser user = null; user = NetmeraUser.getCurrentUser(); if (callback != null) { callback(user, null); } }); try { NetmeraUser.clearSocialSessions((logout, ex) => { if (logout && ex == null) { control.webBrowser.Loaded += new RoutedEventHandler((s, e) => { facebook.getFacebookLoginUri(permissionArray, (loginUri, ex1) => { if (loginUri != null && ex1 == null) { Deployment.Current.Dispatcher.BeginInvoke(() => { control.webBrowser.Navigate(loginUri); }); } else { if (callback != null) { callback(null, ex1); } } }); }); control.webBrowser.LoadCompleted += new LoadCompletedEventHandler((s, e) => { String ss = control.webBrowser.SaveToString(); facebook.getFacebookAccessToken(e.Uri, (accessToken, ex2) => { if (!String.IsNullOrEmpty(accessToken)) { Deployment.Current.Dispatcher.BeginInvoke(() => { popup.IsOpen = false; getFacebookUser((user, ex3) => { if (user != null && ex3 == null) { FacebookSession.save(facebook); } callback(user, ex3); }); }); } else if (ex2 != null) { Deployment.Current.Dispatcher.BeginInvoke(() => { popup.IsOpen = false; if (callback != null) { callback(null, ex2); } }); } }); }); Deployment.Current.Dispatcher.BeginInvoke(() => { popup.Child = control; popup.IsOpen = true; }); } else { if (callback != null) { callback(null, ex); } } }); } catch (Exception) { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_FB_ERROR, "Error occured while logging in facebook.")); } } } else { if (callback != null) { callback(null, new NetmeraException(NetmeraException.ErrorCode.EC_API_KEY_NOT_FOUND, "You didn't set your api key. Please use NetmeraClient.init(apiKey).")); } } }