コード例 #1
0
        internal static JodelWebClient GetJodelWebClientWithHeaders(DateTime time, string stringifiedPayload, string accesstoken = "", bool addBearer = false, HttpMethod method = null)
        {
            JodelWebClient client = new JodelWebClient();

            var headers = Constants.Header;

            headers.Remove("X-Authorization");
            headers.Remove("X-Timestamp");
            headers.Remove("Authorization");

            var keyByte  = Encoding.UTF8.GetBytes(Constants.Key);
            var hmacsha1 = new HMACSHA1(keyByte);

            hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(stringifiedPayload));

            headers.Add("X-Timestamp", $"{time:s}Z");
            headers.Add("X-Authorization", "HMAC " + hmacsha1.Hash.Aggregate(string.Empty, (current, t) => current + t.ToString("X2")));

            if (addBearer)
            {
                headers.Add("Authorization", "Bearer " + accesstoken);
            }

            client.Headers.Add(headers);

            if (method != HttpMethod.Get)
            {
                client.Headers.Add(Constants.JsonHeader);
            }

            client.Encoding = Encoding.UTF8;

            return(client);
        }
コード例 #2
0
ファイル: ApiCall.cs プロジェクト: frzifus/JodelAPI
        internal string ExecuteRequest(User user, Dictionary <string, string> parameters = null, JsonRequest payload = null, string postId = null, WebProxy proxy = null)
        {
            string plainJson     = null;
            string payloadString = payload != null
                ? JsonConvert.SerializeObject(payload, Formatting.None,
                                              new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            })
                : null;

            DateTime dt       = DateTime.Now;
            string   urlParam = Url;

            if (!string.IsNullOrWhiteSpace(postId))
            {
                urlParam += postId;
            }
            if (!string.IsNullOrWhiteSpace(Action))
            {
                urlParam += Action;
            }
            urlParam += ParamsToString(parameters);
            string stringifiedUrl = "https://" + Links.ApiBaseUrl + "/api/" + Version + urlParam;

            string stringifiedPayload = Method.Method + "%" + Links.ApiBaseUrl + "%443%/api/" + Version + urlParam;

            stringifiedPayload += "%" + user.Token.Token;
            stringifiedPayload += "%" + $"{dt:s}Z" + "%%" + payloadString;

            using (var client = JodelWebClient.GetJodelWebClientWithHeaders(dt, stringifiedPayload, user.Token.Token, Authorize, Method))
            {
#if DEBUG
                Console.WriteLine("****************************************************************");
                Console.WriteLine("{0} {1}", Method, stringifiedUrl);
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine("----------------------------------------------------------------");
                for (int i = 0; i < client.Headers.Count; i++)
                {
                    Console.WriteLine("{0,-30}{1}", client.Headers.AllKeys[i], client.Headers.GetValues(i).Aggregate((a, b) => a + ", " + b));
                }
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine(stringifiedPayload);
                Console.WriteLine("----------------------------------------------------------------");
                Console.WriteLine(payloadString);
#endif
                if (proxy != null)
                {
                    client.Proxy = proxy;
                }
                if (Method == HttpMethod.Get)
                {
                    plainJson = client.DownloadString(stringifiedUrl);
                }
                else
                {
                    plainJson = client.UploadString(stringifiedUrl, Method.Method, payloadString ?? string.Empty);
                }
#if DEBUG
                //Console.WriteLine("----------------------------------------------------------------");
                //Console.WriteLine(plainJson);
                Console.WriteLine("****************************************************************");
#endif
            }

            return(plainJson);
        }