之前寫過幾篇文章說到,關於透過 API TOKEN 去修改 Cloudflare 的 DNS 紀錄 ,有時候在 Cloudflare 有點麻煩的是 如果修改一張圖片會因為有 cloudflare CDN cache 的問題,這是好事也是壞事,之後我想到一個方法,就是如果在準備修改之前,當然你可以設計在進入到客戶在進入管理後台的時候,偷偷的先打開 development mode ,反正開始後三小時會關閉 development code 機制,這樣也是一個好方法,這樣就可以閃躲 cache 問題…

之後這兩個參數也要開
不然你會遇到 回應訊息 是這樣的錯誤
"{"success":false,"errors":[{"code":9109,"message":"Unauthorized to access requested resource"}],"messages":[],"result":null}"
1. 查看 是否開啟 Development Mode
我整理得很簡單,直接看 Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpClient clientGetZones = new HttpClient(); | |
clientGetZones.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
clientGetZones.DefaultRequestHeaders.Add("Authorization", " Bearer API_TOKEN"); | |
var url1 = "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/settings/development_mode"; | |
/* | |
curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/settings/development_mode" \ | |
-H "X-Auth-Email: user@example.com" \ | |
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ | |
-H "Content-Type: application/json" | |
*/ | |
/* | |
Response Result | |
{ | |
"success": true, | |
"errors": [], | |
"messages": [], | |
"result": { | |
"id": "development_mode", | |
"value": "off", | |
"editable": true, | |
"modified_on": "2014-01-01T05:20:00.12345Z", | |
"time_remaining": 3600 | |
} | |
} | |
*/ | |
var res = clientGetZones.GetAsync(url1).Result.Content.ReadAsStringAsync().Result; | |
// true for open , false for close | |
var isOpenDeveloperMode=JObject.Parse(res)["result"]["value"].ToString()!= "off"; |
2. 開啟或是關閉 ,這裡面就是切換 on or off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpClient clientGetZones = new HttpClient(); | |
clientGetZones.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
clientGetZones.DefaultRequestHeaders.Add("Authorization", " Bearer API_TOKEN"); | |
var url1 = "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/settings/development_mode"; | |
/* | |
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/settings/development_mode" \ | |
-H "X-Auth-Email: user@example.com" \ | |
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ | |
-H "Content-Type: application/json" \ | |
--data '{"value":"off"}' | |
*/ | |
HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), url1); | |
// For Close Dev Mode. | |
//requestMessage.Content = new StringContent("{\"value\":\"off\"}", Encoding.UTF8, "application/json"); | |
// For Open Dev Mode. | |
requestMessage.Content = new StringContent("{\"value\":\"on\"}", Encoding.UTF8, "application/json"); | |
HttpResponseMessage response = clientGetZones.SendAsync(requestMessage).GetAwaiter().GetResult(); | |
var res = response.Content.ReadAsStringAsync().Result.ToString(); | |
//Response : | |
//{"result":{"id":"development_mode","value":"on","modified_on":"2020-11-27T08:36:27.345871Z","time_remaining":10800,"editable":true},"success":true,"errors":[],"messages":[]} |
整理一下,因為有需要所以筆記一下給需要的人,也怕我自己忘記…
---
請你暫時把你的勇氣給我 在夢想快消失的時候 讓我的 Code 用力的穿過天空 為愛我的人做一秒英雄 如果這篇文章有幫助到您,簡單留個言,或是幫我按個讚,讓我有寫下去的動力...