Ejemplo n.º 1
0
        public static async Task <HttpResponseMessage> SendRestRequest(AuthenticationAgent authAgent, HttpMethod method, string path, object body, Dictionary <string, string> query = null, bool logRequest = false, string contentType = @"text/csv")
        {
            //Load the headers
            Dictionary <string, string> headers = new Dictionary <string, string>()
            {
                { "Authorization", "Bearer " + authAgent.authToken.access_token }
            };
            //Create the endpoint
            string endpoint = GetEndpoint(authAgent, path);

            if (method != HttpMethod.Put)
            {
                //Convert the object to json
                var jsonSettings = new JsonSerializerSettings
                {
                    //Converters = { new FormatNumbersAsTextConverter() },
                    NullValueHandling = NullValueHandling.Ignore
                };
                string json = JsonConvert.SerializeObject(body, jsonSettings);

                if (logRequest)
                {
                    string prettyJson = JsonConvert.SerializeObject(body, Formatting.Indented, jsonSettings);
                    Console.WriteLine(prettyJson);
                }

                return(await HttpAgent.Request(method, endpoint, query, json, headers));
            }
            else
            {
                return(await HttpAgent.UploadFile(endpoint, (String)body, headers, contentType));
            }
        }
        public async Task RefreshAuthToken()
        {
            byte[] tokenBytes     = System.Text.Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret);
            string codeAuthBase64 = System.Convert.ToBase64String(tokenBytes);

            var body = "grant_type=refresh_token&refresh_token=" + authToken.refresh_token;
            Dictionary <string, string> headers = new Dictionary <string, string>()
            {
                { "Authorization", "Basic " + codeAuthBase64 }
            };

            HttpResponseMessage response = await HttpAgent.Request(HttpMethod.Post, RestApiUrl + TokenEndpoint, null, body, headers, "application/x-www-form-urlencoded");

            string json = await response.Content.ReadAsStringAsync();

            authToken = JsonConvert.DeserializeObject <AuthToken>(json);

            if (!response.IsSuccessStatusCode ||
                authToken.access_token == null ||
                authToken.expires_in == 0 ||
                authToken.refresh_token == null ||
                authToken.token_type == null)
            {
                SetError(json);
            }
        }
        public void GetAuthUrl()
        {
            Dictionary <string, string> query = new Dictionary <string, string>()
            {
                { "response_type", "code" },
                { "scope", "signature" },
                { "client_id", ClientId },
                { "state", "abc123" },
                { "redirect_uri", RedirectUrl }
            };

            authUrl = RestApiUrl + OAuthEndpoint + HttpAgent.BuildQuery(query);
        }
        public async Task GetUserInfo()
        {
            Dictionary <string, string> headers = new Dictionary <string, string>()
            {
                { "Authorization", "Bearer " + authToken.access_token }
            };
            HttpResponseMessage response = await HttpAgent.Request(HttpMethod.Get, RestApiUrl + UserInfoEndpoint, null, "", headers);

            string json = await response.Content.ReadAsStringAsync();

            userInfo = JsonConvert.DeserializeObject <UserInfo>(json);

            if (!response.IsSuccessStatusCode ||
                userInfo.accounts == null)
            {
                SetError(json);
            }
        }