POST request in Apple News API

189 views Asked by At

I am struggling with the POST request for creating an article.

Can anybody please provide me an example value of the canonical request in C#?

The error I receive is signature issue:

{"errors":[{"code":"WRONG_SIGNATURE"}]}

From what I learnt a canonical request is build from:

Method = POST
URL = https://news-api.apple.com/channels/ChanelID/articles
Date = 2019-07-24T18:12:32Z
Content-Type = multipart/form-data
Body - Not fully understood

The Apple News API documentation says

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 - What is meant by this?

I want to post one test article from the json files provided in the apple's documentation.

This is the class generating the auth header:

public class Security
{
  public static string AuthHeader(string method, string url, object content=null)
  {
        string formDataBoundary = String.Format("{0:N}", Guid.NewGuid());
        var apiKeyId = Constants.AppleNewsKeyId;
        var apiKeySecret = Constants.AppleNewsKeySecret;
        if (string.IsNullOrEmpty(apiKeyId) || string.IsNullOrEmpty(apiKeySecret)) return string.Empty;
        var encoding = new ASCIIEncoding();
        var dt = DateTime.Now.ToString(Constants.DateFormat);
        var canonicalRequest = string.Format("{0}{1}{2}{3}", method, url, dt, content);
        var key = Convert.FromBase64String(apiKeySecret);
        var hmac = new HMACSHA256(key);
        var hashed = hmac.ComputeHash(encoding.GetBytes(canonicalRequest));
        var signature = Convert.ToBase64String(hashed);
        var authorizaton = string.Format(@"HHMAC; key={0}; signature={1}; date={2}", apiKeyId, signature, dt);
        return authorizaton;
    }
}

This is the constants class:

public static class Constants
{
    public static readonly string ChannelId = "myID"; 
    public static readonly string AppleNewsBaseUri = "https://news-api.apple.com";
    public static readonly string DateFormat = "yyyy-MM-ddTHH:mm:ssK";
    public static readonly string AppleNewsKeySecret = "myID";
    public static readonly string AppleNewsKeyId = "myID";   
}

And the action class which is missing code in order to have proper content instead of null passed to the AuthHeader method:

 public class Action
    {
        public static string SendCommand(string action, string method)
        {
         var url = $"{Constants.AppleNewsBaseUri}{action}" + "/articles";
        //string content = String.Format("{0}:{1}", "Content-Disposition", "form-data; filename=article.json; name=article.json;");
        var authheader = Security.AuthHeader(method, url, null);
        var request = WebRequest.Create(url);
        request.PreAuthenticate = true;
        request.Method = "POST";
        request.Headers.Add("Authorization", authheader);

        if (method.Equals("post", StringComparison.InvariantCultureIgnoreCase))
            request.ContentType = "multipart/form-data;" ;
        var output = string.Empty;
        try
        {
            string filePath = Path.GetFullPath("article.json");
            using (StreamReader r = new StreamReader(filePath))
            {
                string json = r.ReadToEnd();
                dynamic jsonObj = JsonConvert.DeserializeObject(json);

                ASCIIEncoding encoding = new ASCIIEncoding();
                Byte[] bytes = encoding.GetBytes(json);
                request.ContentLength = bytes.Length;

                Stream newStream = request.GetRequestStream();
                newStream.Write(bytes, 0, bytes.Length);
                newStream.Close();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }

        try
        {
            using (var response = request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                    output = reader.ReadToEnd();
            }
        }
        catch (WebException e)
        {
            using (var reader = new StreamReader(e.Response.GetResponseStream()))
            {
                output = reader.ReadToEnd();
            }
        }
        return output;
    }


    public static string ReadChannel()
    {
        var action = $"/channels/{Constants.ChannelId}";
        const string method = "POST";
        return SendCommand(action, method);
    }
}
0

There are 0 answers