亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 c# 連接到 Apple News API?

如何從 c# 連接到 Apple News API?

C#
守候你守候我 2021-08-07 17:59:14
我正在嘗試連接到 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 關注
  • 186 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號