using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace CrawRobot.Class { public static class GetTokenHelper { // 百度云中开通对应服务应用的 API Key private static string clientId = "1xkwSoQsx0GE6Fm99tzbflAL";//"PMIzFi2xhwSMXgiYqEfGIGx7"; // 百度云中开通对应服务应用的 Secret Key private static string clientSecret = "A86P8sSdMaYBnRUTD2GQk4i8FcIlEFlR";//"SiLX7KgPZnGKtqOoZCzRw7dakYCt1zrs"; public static string getAccessToken() { string authHost = "https://aip.baidubce.com/oauth/2.0/token"; HttpClient client = new HttpClient(); List> paraList = new List>(); paraList.Add(new KeyValuePair("grant_type", "client_credentials")); paraList.Add(new KeyValuePair("client_id", clientId)); paraList.Add(new KeyValuePair("client_secret", clientSecret)); HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result; string result = response.Content.ReadAsStringAsync().Result; JObject jo = null; var joResult = ""; jo = (JObject)JsonConvert.DeserializeObject(result); joResult = jo["access_token"].ToString(); return joResult; } } }