我正在嘗試連接到 Apple 的新聞 API。生成簽名時,每個不同示例的結果都相同。但是,我不斷收到此錯誤: {"errors":[{"code":"WRONG_SIGNATURE"}]}這是我生成身份驗證標頭的 c# 代碼。public class Security{ public static string AuthHeader(string method, string url, object content=null) { var apiKeyId = "<YOUR KEY HERE...>"; //we get this value from our settings file. var apiKeySecret = "<YOUR SECRET HERE...>"; //we get this value from our settings file. 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}", method, url, dt); 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; }}常量類的簡短版本public static class Constants{ public static readonly string ChannelId = "<YOUR CHANNEL ID HERE...>"; //again from our settings file public static readonly string BaseUrl = "https://news-api.apple.com"; public static readonly string DateFormat = "yyyy-MM-ddTHH:mm:ssK"; }Actions 類的簡短版本(SendCommand 是執行請求的方法)public class Actions{ public static string SendCommand(string action, string method) { var url = $"{Constants.BaseUrl}{action}"; var authheader = Security.AuthHeader(method, url, null); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Timeout = 1000; request.Headers.Add("Authorization", authheader); request.Accept = "application/json"; request.ContentType = "application/json"; var output = string.Empty;我正在使用 ReadChannel 方法進行測試。我也嘗試過 php 和 ruby 中的示例,但沒有成功。任何想法如何正確地做到這一點?
- 2 回答
- 0 關注
- 186 瀏覽
添加回答
舉報
0/150
提交
取消