Exemplo n.º 1
0
        #pragma warning restore 0649

        #region 获取百度 access token
        public static string GetBaiduToken(string api_key, string secret_key)
        {
            try
            {
                string     url    = "https://aip.baidubce.com/oauth/2.0/token";
                string     data   = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}", api_key, secret_key);
                string     result = HttpClient.Post(data, url);
                BaiduToken token  = JsonConvert.DeserializeObject <BaiduToken>(result);
                if (string.IsNullOrEmpty(token.error) == false)
                {
                    return("错误:" + token.error + ":" + token.error_description);
                }
                else
                {
                    return(token.access_token.Trim());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
 private void ButtonApiTest_Click(object sender, EventArgs e)
 {
     try
     {
         string     url    = "https://aip.baidubce.com/oauth/2.0/token";
         string     data   = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}", TextBoxApiKey.Text, TextBoxSecretKey.Text);
         string     result = HttpClient.Post(data, url);
         BaiduToken token  = JsonConvert.DeserializeObject <BaiduToken>(result);
         if (string.IsNullOrEmpty(token.error) == false)
         {
             MessageBox.Show(this, token.error + ":" + token.error_description, "错误");
         }
         else
         {
             TextBoxToken.Text = token.access_token.Trim();
             MessageBox.Show(this, "已生成密钥,有效期30天!", "提示");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "错误");
     }
 }