dears , ineed urgent help for the below:
i have problem when use apple news API using c# code
,and always the API return wrong signature i use the below code
string appleDate = String.Format("{0}Z", DateTime.UtcNow.ToString("s"));
string url = "https://news-api.apple.com/channels/" + channel_id;
string canonical_request = "GET" + url + appleDate;
//decode the api_key_secret
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(api_key_secret);
string enc = Convert.ToBase64String(encbuff);
string key = Server.UrlEncode(enc);
//genrate hash mac
//call method to generate has mac
string hash = CreateSignature(api_key_secret, canonical_request);
public static string CreateSignature(string secret, string canURL)
{
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(canURL);
using (var hmacsha1 = new HMACSHA1(keyByte,false))
{
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
string hashstring = BitConverter.ToString(hmacsha1.ComputeHash(messageBytes)).Replace("-", "").ToLower();
return Convert.ToBase64String(encoding.GetBytes(hashstring));
}
}
//make the request
try
{
using (HttpClient client = new HttpClient())
{
using (StringContent jsonContent = new StringContent(json.ToString()))
{
jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(string.Format("HHMAC; key={0}; signature={1}; date={2}", api_key_id, hash, appleDate));
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", string.Format("HHMAC; key={0}; signature={1}; date={2}", api_key_id, hash, appleDate));
using (HttpResponseMessage response = await client.PostAsync(url, jsonContent))
{
var reponseString = await response.Content.ReadAsStringAsync();
}
}
}
}
catch (Exception ex)
{
}