将文章发布到apple news api

时间:2017-06-30 07:06:38

标签: c# .net

我正在尝试将文章发布到apple news api。 Apple指示执行以下操作:

创建请求的规范版本,作为以下字节顺序连接:

HTTP方法(例如,全部大写的GET或POST)

请求的完整网址

ISO 8601格式的当前日期 如果请求是POST请求并且包含实体,请包含以下内容:

Content-Type标头的值

实体的全部内容

将API密钥的密钥从Base64解码为原始字节。 使用HMAC SHA-256通过规范请求使用解码的API密钥秘密创建哈希。 使用Base64对哈希进行编码。 将Authorization标头设置为:

授权:HHMAC;键=;签名=; date = where是步骤1中的日期字符串。 发送请求。

以下是我的c#代码

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();
}

解码方法

public static string base64Decode(string data)
{
    var base64EncodedBytes = System.Convert.FromBase64String(data);
    return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}

HmacSha256扩展方法是

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();
    }
}

收到了UNAuthorized错误(签名错误)。

0 个答案:

没有答案