Example #1
0
        /// <summary>
        /// Gets a token based on the provided info.  Make sure to set the target file first.
        /// </summary>
        public async Task <string> Authenticate()
        {
            token = "";
            string resultJson;
            string url = string.Empty;

            url = BaseUrl + "databases/" + CurrentDatabase + "/sessions";
            var byteArray = Encoding.ASCII.GetBytes(FMAccount + ":" + FMPassword);
            var sig       = Convert.ToBase64String(byteArray);

            webClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", sig);

            var content = new StringContent("{}", Encoding.UTF8, "application/json");

            try
            {
                HttpResponseMessage response = null;

                // empty json as the body as per the api documentation, probably works without a body too as per my Postman testing

                // works in 17 but not 18
                response = await webClient.PostAsync(url, content);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    // get the json from the response body
                    resultJson = await response.Content.ReadAsStringAsync();

                    // token is in the response body
                    // body will also contain an error code :

                    /*
                     *  {
                     *      "response": {
                     *          "token": "3eaed56b71f0ba0c6ca8d2984e7a681e3bbada56d48b1b21ab4"
                     *      },
                     *      "messages": [
                     *          {
                     *              "code": "0",
                     *              "message": "OK"
                     *          }
                     *      ]
                     *  }
                     */
                    Received received = JsonConvert.DeserializeObject <Received>(resultJson);
                    if (received.messages[0].code == "0")
                    {
                        token = received.Response.Token;

                        // clear the default headers to get rid of the auth header since we'll be re-using webclient
                        webClient.DefaultRequestHeaders.Clear();

                        // add the new header
                        webClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                    }
                }
                else
                {
                    resultJson = await response.Content.ReadAsStringAsync();

                    Received received = JsonConvert.DeserializeObject <Received>(resultJson);
                    this.lastErrorCode = Convert.ToInt16(received.messages[0].code);
                }
            }
            catch (Exception ex)
            {
                //If it fails it could be because of the TLS issue
                token = "Error - " + ex.Message;
            }
            return(token);
        }
Example #2
0
 private void SetLastScriptErrors(Received received)
 {
     lastErrorCodeScript           = Convert.ToInt32(received.Response.ScriptError);
     lastErrorCodeScriptPreRequest = Convert.ToInt32(received.Response.ScriptErrorPreRequest);
     lastErrorCodeScriptPreSort    = Convert.ToInt32(received.Response.ScriptErrorPreSort);
 }
Example #3
0
 private void SetFMSerror(Received r)
 {
     this.lastErrorMessage = r.messages[0].message;
     this.lastErrorCode    = Convert.ToInt32(r.messages[0].code);
 }