Exemplo n.º 1
0
        private object RequestEndpoint(string endpoint, string method, object query, object body)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var request = HttpHelper.GetWebRequest(endpoint, method, query);

            request.Headers.Add("Authorization", string.Format("Bearer {0}", Token));
            request.Headers.Add("api-sdk", string.Format("dotnet-core-{0}", version));
            if (PartnerToken != null)
            {
                request.Headers.Add("partner-token", PartnerToken);
            }

            try {
                return(HttpHelper.SendRequest(request, body));
            } catch (WebException e) {
                if (e.Response != null && e.Response is HttpWebResponse)
                {
                    var statusCode = (int)((HttpWebResponse)e.Response).StatusCode;
                    var reader     = new StreamReader(e.Response.GetResponseStream());
                    throw GnException.Build(reader.ReadToEnd(), statusCode);
                }

                throw GnException.Build("", 500);
            }
        }
Exemplo n.º 2
0
        private void Authenticate()
        {
            object body = new {
                grant_type = "client_credentials"
            };

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var request     = HttpHelper.GetWebRequest("/authorize", "post", null);
            var credentials = string.Format("{0}:{1}", ClientId, ClientSecret);
            var encodedAuth = Convert.ToBase64String(Encoding.GetEncoding("UTF-8").GetBytes(credentials));

            request.Headers.Add("Authorization", string.Format("Basic {0}", encodedAuth));
            request.Headers.Add("api-sdk", string.Format("dotnet-core-{0}", version));

            try {
                var response = HttpHelper.SendRequest(request, body);
                Token = response.access_token;
            } catch (WebException) {
                throw GnException.Build("", 401);
            }
        }