我正在尝试连接到 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 回答
- 2 回答
- 0 关注
- 173 浏览
添加回答
举报
0/150
提交
取消