Пример #1
0
        /// <summary>
        /// Adds social credentials to an existing account
        /// </summary>
        /// <param name="login">Account to associate the social login with</param>
        /// <returns>true if successful, false otherwise</returns>
        /// <exception cref="ArgumentException">If insufficient information to associate account</exception>
        public bool AssociateSocialAccount(LoginAccount login)
        {
            if (this.ValidateSocial() == false)
            {
                throw new ArgumentException("Insufficient information to associate account");
            }

            try
            {
                RequestInfo req = new RequestInfo();
                req.RequestContentType = "application/json";
                req.AcceptContentType = "application/json";
                req.HttpMethod = "PUT";
                req.ApiPassword = this.ApiPassword;
                req.LoginEmail = login.Email;
                req.Uri = string.Format("{0}/users/{2}/social", login.BaseUrl, login.AccountId, login.UserId);
                req.IntegratorKey = RestSettings.Instance.IntegratorKey;

                RequestBuilder utils = new RequestBuilder();
                utils.Proxy = this.Proxy;

                List<RequestBody> requestBodies = new List<RequestBody>();
                RequestBody rb = new RequestBody();

                rb.Text = this.ConstructAssociateJson();

                if (string.IsNullOrEmpty(rb.Text) == true)
                {
                    return false;
                }

                requestBodies.Add(rb);

                req.RequestBody = requestBodies.ToArray();
                utils.Request = req;
                ResponseInfo response = utils.MakeRESTRequest();

                this.Trace(utils, response);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    this.ParseErrorResponse(response);
                }

                return response.StatusCode == HttpStatusCode.OK;
            }
            catch
            {
                return false;
            }
        }
Пример #2
0
        /// <summary>
        /// Used to update account's userId, which is missing when using oauth (O365 etc.)
        /// </summary>
        public void updateAllAccountsInfo()
        {
            if (LoginAccounts == null)
            {
                LoginAccounts = new Logins();
            }
            RequestInfo req = new RequestInfo();
            RequestBuilder utils = new RequestBuilder();

            req.RequestContentType = "application/json";
            req.AcceptContentType = "application/json";
            req.HttpMethod = "GET";
            req.LoginEmail = this.Email;
            req.LoginPassword = string.IsNullOrEmpty(this.ApiPassword) == false ? this.ApiPassword : this.Password;
            req.DistributorCode = RestSettings.Instance.DistributorCode;
            req.DistributorPassword = RestSettings.Instance.DistributorPassword;
            req.IntegratorKey = RestSettings.Instance.IntegratorKey;
            int index = this.BaseUrl.LastIndexOf('/') - 9;
            string url = this.BaseUrl.Substring(0, index);
            req.Uri = string.Format("{0}/login_information", url);

            utils.Request = req;
            utils.Proxy = this.Proxy;

            ResponseInfo response = utils.MakeRESTRequest();

            this.Trace(utils, response);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                this.ParseErrorResponse(response);
                return;
            }

            var accounts = new List<LoginAccount>();
            JObject json = JObject.Parse(response.ResponseText);
            foreach (JObject j in json["loginAccounts"])
            {
                var loginAccount = new LoginAccount();
                loginAccount.Name = (string)j["name"];
                loginAccount.UserId = (string)j["userId"];
                loginAccount.UserName = (string)j["userName"];
                loginAccount.AccountId = (string)j["accountId"];
                loginAccount.BaseUrl = (string)j["baseUrl"];
                loginAccount.IsDefault = (bool)j["isDefault"];
                loginAccount.Email = (string)j["email"];
                accounts.Add(loginAccount);
            }
            this.LoginAccounts.LoginAccounts = accounts.ToArray();
        }