1、得到企業微信管理端權限,登陸企業企業微信管理端界面,並建立應用,以下圖中的【網站消息推送】前端
2、參見企業微信API文獻,根據corpid=ID&corpsecret=SECRET(其中企業微信ID即爲corpid,Secret即爲corpsecret),便可獲取access_token,此時能夠使用Postman進行測試ajax
3、關鍵部分,網站代碼部分json
--------------------------------前端的行爲----------------------------------------------------------------------------------c#
1)前端能夠使用ajax寫一個指向controller的請求,以下api
$(document).ready(function () {
$("#message").click(function () {
$.ajax({
type: "GET",
url: "ECModel/GetAccessToken",
success: function (data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error");
}
});跨域
});
});服務器
--------------------------------控制器中的行爲-------------------------------------------------------------------------------微信
2)在控制器中實現如下操做(藍色文本須要自行確認修改)app
/// <summary>
/// 接收前端向企業微信發送消息的請求,向企業微信得到許可憑證
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult<string> GetAccessToken()
{ //GET獲取accessToken的參數 corid
string uri = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=(替換爲本身企業微信的ID)&corpsecret=(替換爲本身應用的密鑰)";
//建立請求
WebRequest request = WebRequest.Create(uri);
//請求設置
request.Credentials = CredentialCache.DefaultCredentials;
//建立應答接收
WebResponse response = request.GetResponse();
//建立應答讀寫流
string accessToken;
using (Stream streamResponse=response.GetResponseStream())
{
StreamReader reader = new StreamReader(streamResponse);
string responseFromServer = reader.ReadToEnd();
JObject res = (JObject)JsonConvert.DeserializeObject(responseFromServer);
accessToken = res["access_token"].ToString();
reader.Close();
}
//得到許可證憑證
PostMail(accessToken);
//關閉響應
response.Close();
return "success";
}
/// <summary>
/// 得到許可憑證後向企業微信發送消息
/// </summary>
/// <param name="accessToken">許可憑證</param>
public void PostMail(string accessToken)
{ //POST的API
string uri = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
//建立請求
WebRequest myWebRequest = WebRequest.Create(uri);
//請求設置
myWebRequest.Credentials = CredentialCache.DefaultCredentials;
myWebRequest.ContentType = "application/json;charset=UTF-8";
myWebRequest.Method = "POST";
//向服務器發送的內容
using (Stream streamResponse = myWebRequest.GetRequestStream())
{
//建立JSON格式的發送內容
JObject postedJObject = new JObject
{
//在此處設置發送內容及對象
{ "touser", "Heavy" },
{ "msgtype", "text" },
{ "agentid", 1000002 }
};
JObject text = new JObject
{
{"content","內容來自網站--內容可自行編輯--heavy"}
};
postedJObject.Add("text", text);
postedJObject.Add("safe", 0);
//將傳送內容編碼
String paramString = postedJObject.ToString(Newtonsoft.Json.Formatting.None, null);
byte[] byteArray = Encoding.UTF8.GetBytes(paramString);
//向請求中寫入內容
streamResponse.Write(byteArray, 0, byteArray.Length);
}
//建立應答
WebResponse myWebResponse = myWebRequest.GetResponse();
//建立應答的讀寫流
string responseFromServer;
using (Stream streamResponse = myWebResponse.GetResponseStream())
{
StreamReader streamRead = new StreamReader(streamResponse);
responseFromServer = streamRead.ReadToEnd();
}
//關閉應答
myWebResponse.Close();
}post
4、小結
在控制器中實現微信API訪問,能夠避免跨域訪問的問題,固然也能夠在前端用JS實現,JS中實現須要考慮跨域問題