BISDK.Client.Authenticate C# (CSharp) Method

Authenticate() public method

public Authenticate ( string email, string password ) : Response
email string
password string
return Response
        public Response Authenticate(string email, string password)
        {
            RestRequest request = new RestRequest("_oauth2/token", Method.POST);
            request.AddParameter("client_id", ClientId);
            if (!String.IsNullOrEmpty(email))
            {
                Email = email;
                request.AddParameter("username", email);
            }

            if (!String.IsNullOrEmpty(password))
                request.AddParameter("password", password);

            request.AddParameter("grant_type", "password");

            //clear old access token, we don't want access token in this request
            AccessToken = null;
            this.Authenticator = null;

            if (String.IsNullOrEmpty(ClientSecret))
                throw new Exception("Client secret cannot be blank");

            request.AddParameter("client_secret", ClientSecret);

            Response response = Execute(request);
            JObject responseObject = response.JObject;

            AccessToken = (string)responseObject["access_token"];

            if (responseObject["refresh_token"]!=null)
            {
                RefreshToken = (string)responseObject["refresh_token"];
            }

            if (CustomDomain==null && responseObject["systems"] != null)
            {
                JArray Systems = (JArray)responseObject["systems"];
                if (Systems.Count > 0)
                {
                    this.BaseUrl = "https://" + Systems[0]["host_name"];
                }
            }

            return response;
        }

Usage Example

        public AuthenticationExample()
        {
            string ClientId = "ClientID";
            string ClientSecret = "SECRET";
            string Email = "*****@*****.**";
            string Password = "******";

            //Create the client
            client = new Client(ClientId, ClientSecret);
            //client.CustomDomain="auth.brightideasandbox.com";

            //Authenticate with user's email and password
            Response authResponse = client.Authenticate(Email, Password);

            //Make API request to pull the member list
            Request request = new Request("member", ApiAction.INDEX);
            Response response = client.Execute(request);

            /////////////////////////////////////////
            // There are 3 ways to read the response
            /////////////////////////////////////////

            //1.Read data by using JSON.Net
            JObject json = response.JObject;
            JArray memberList = (JArray)json["member_list"];

            System.Diagnostics.Debug.WriteLine(memberList.Count);

            //2.Read data by deserializing the json
            Dictionary<string, object> dictionary = response.Deserialize<Dictionary<string, object>>();

            //3.Get the content as string
            string content = response.Content;
        }