3.之後就是程式碼的部分,這邊我就直接分享出來,請注意 Authorization 的 value 得部分空白很重要,多一個少一個空白都會出現 {"code":10000,"message":"Authentication error"}] ,被這搞到一下子,我都想說我按照文件做都還不行..
Response Model:
public class Meta
{
public bool auto_added { get; set; }
public bool managed_by_apps { get; set; }
public bool managed_by_argo_tunnel { get; set; }
public string source { get; set; }
}
public class Result
{
public string id { get; set; }
public string zone_id { get; set; }
public string zone_name { get; set; }
public string name { get; set; }
public string type { get; set; }
public string content { get; set; }
public bool proxiable { get; set; }
public bool proxied { get; set; }
public int ttl { get; set; }
public bool locked { get; set; }
public Meta meta { get; set; }
public DateTime created_on { get; set; }
public DateTime modified_on { get; set; }
}
public class ResultInfo
{
public int page { get; set; }
public int per_page { get; set; }
public int count { get; set; }
public int total_count { get; set; }
public int total_pages { get; set; }
}
public class DNSRecordResult
{
public List result { get; set; }
public bool success { get; set; }
public List
Get All DNS Records C# Code:
HttpClient clientGetZones = new HttpClient();
clientGetZones.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//clientGetZones.DefaultRequestHeaders.Add("Authorization", " Bearer [API TOKEN]");
clientGetZones.DefaultRequestHeaders.Add("Authorization", " Bearer abcdedghijklmnopQRSTUV_ABCD_ggggPqCD");
//var url1 = "https://api.cloudflare.com/client/v4/zones/[ZONE ID]/dns_records?type=A&page=1&per_page=1000&order=type&direction=desc&match=all";
//取得 A 紀錄的,並且一次取1000 筆回來 :P
var url1 = "https://api.cloudflare.com/client/v4/zones/2836714271b942af9386532b00b5579c/dns_records?type=A&page=1&per_page=1000&order=type&direction=desc&match=all";
var res = clientGetZones.GetAsync(url1).Result.Content.ReadAsStringAsync().Result;
var dnsRecords = JsonConvert.DeserializeObject(res);
foreach (var record in dnsRecords.result)
{
ltlResult.Text += record.id + "- " + record.name + "(" + record.type + ")" + ":" + record.content + "," + record.created_on.ToString("yyyy-MM-dd") + ", is Proxied:" + record.proxied + " ";
}
//Result
//76698b3a5a5572d16d7a55db3e081099- sample.com(A):163.205.119.3,2020-10-22, is Proxied:True
//3bdcb03125648d36f3751d58017ba920- wizard.sample.com(A):163.205.171.212,2020-10-23, is Proxied:True
//213c334d631e6dae116d7dff61e1e0dd- demo1.sample.com(A):163.13.202.11,2020-10-27, is Proxied:True