public string makeSignature(GetPostType calltype, string action, ref string stringtimestamp, string accessKey, string secureKey /*, string apiKey*/) { if (string.IsNullOrEmpty(action)) { return("parameter error"); } long timestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; string space = " "; string newLine = "\n"; string method = calltype == GetPostType.POST ? "POST" : "GET"; stringtimestamp = timestamp.ToString(); string message = new StringBuilder() .Append(method) .Append(space) .Append(action) .Append(newLine) .Append(stringtimestamp) .Append(newLine) //.Append(apiKey) //.Append(newLine) .Append(accessKey) .ToString(); byte[] secretKey = Encoding.UTF8.GetBytes(secureKey); HMACSHA256 hmac = new HMACSHA256(secretKey); hmac.Initialize(); byte[] bytes = Encoding.UTF8.GetBytes(message); byte[] rawHmac = hmac.ComputeHash(bytes); return(Convert.ToBase64String(rawHmac)); }
public async Task <string> WebApiCall(string Url, GetPostType calltype, string action, List <KeyValuePair <string, string> > parameters) { Config config = Config.Instance; string accessKey = config.GetEnumValue(Category.Config, Key.ApiGatewayAccessKey); string secureKey = config.GetEnumValue(Category.Config, Key.ApiGatewaySecretKey); //string apiKey = config.GetEnumValue(Category.Config, Key.ApiGatewayKey); string responseString = string.Empty; try { foreach (var a in parameters) { nlog.Warn(string.Format("key : {0}, value :{1}", a.Key, a.Value)); } using (HttpClient client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(30000); string timestamp = string.Empty; string sig = Auth.Instance.makeSignature(calltype, action, ref timestamp, accessKey, secureKey /*, apiKey*/); string url = Url + action; client.DefaultRequestHeaders.Add("x-ncp-apigw-timestamp", timestamp); //client.DefaultRequestHeaders.Add("x-ncp-apigw-api-key", apiKey); client.DefaultRequestHeaders.Add("x-ncp-iam-access-key", accessKey); client.DefaultRequestHeaders.Add("x-ncp-apigw-signature-v1", sig); if (calltype == GetPostType.POST) { var content = new FormUrlEncodedContent(parameters); var response = await client.PostAsync(url, content); responseString = await response.Content.ReadAsStringAsync(); } else { var response = await client.GetAsync(url); responseString = await response.Content.ReadAsStringAsync(); } } } catch (Exception ex) { responseString = ex.Message; nlog.Error(string.Format("{0}, {1}", ex.Message, ex.StackTrace)); } return(responseString); }
public async Task <string> WebApiCall(string url, GetPostType calltype, string action) { return(await WebApiCall(url, calltype, action, "")); }