I am trying to post articles to Apple News API but I am getting the following error:
Unauthorized Error (Wrong signature).
Apple instructs to do the following:
Create a canonical version of the request as a byte-wise concatenation of the following:
The HTTP method (for example, GET or POST, in all caps)
The full URL of the request
The current date in ISO 8601 format If the request is a POST request and it includes an entity, include the following:
The value of the Content-Type header
The full content of the entity
Decode the API key’s secret from Base64 to raw bytes. Create the hash using HMAC SHA-256 over the canonical request with the decoded API key secret. Encode the hash with Base64. Set the Authorization header as:
Authorization: HHMAC; key=; signature=; date= where is the date string from step 1. Send the request.
Below is my C# code:
string path = "https://news-api.apple.com/channels/channelid/articles";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(path);
httpWebRequest.ContentType = "multipart/form-data";
httpWebRequest.Method = "POST";
httpWebRequest.Accept = "application / json";
httpWebRequest.Host = "news-api.apple.com";
httpWebRequest.UseDefaultCredentials = true;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
httpWebRequest.KeepAlive = true;
string appleDate= String.Format("{0}Z", DateTime.UtcNow.ToString("s"));
string credentials = String.Format("{0}:{1}", "Content-Disposition", "form-data; ");
credentials += String.Format("{0}:{1}", "filename", "article.json; ");
credentials += String.Format("{0}:{1}", "name", "article.json; ");
credentials += String.Format("{0}:{1}", "Authorization", "HHMAC; ");
credentials += String.Format("{0}:{1}", "key", "api key; ");
string decodedSecret = base64Decode("secret_key");
string canonical_request = "POST" + path + appleDate;
string hash = Class1.HmacSha256Digest(canonical_request, decodedSecret);
string Encodedhash = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(hash));
credentials += String.Format("{0}:{1}", "signature", Encodedhash + "; ");
credentials += String.Format("{0}:{1}", "date", appleDate + "; ");
httpWebRequest.Headers.Add("Authorization", "Basic " +credentials);
httpWebRequest.Credentials = new NetworkCredential("xxx.com", "xxxx");
using (StreamReader r = new StreamReader(HttpContext.Current.Server.MapPath("~/article.json")))
{
string json = r.ReadToEnd();
dynamic jsonObj = JsonConvert.DeserializeObject(json);
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(json);
Stream newStream = httpWebRequest.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = newStreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
Decode method:
public static string base64Decode(string data)
{
var base64EncodedBytes = System.Convert.FromBase64String(data);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
HmacSha256 extension method:
public static class Class1
{
public static string HmacSha256Digest(this string message, string secret)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] keyBytes = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
System.Security.Cryptography.HMACSHA256 cryptographer = new System.Security.Cryptography.HMACSHA256(keyBytes);
byte[] bytes = cryptographer.ComputeHash(messageBytes);
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
}
I've got an
Unauthorized Error (Wrong signature).