【学习】调用金蝶登录API接口原创
9人赞赏了该文章
646次浏览
编辑于2024年09月02日 11:32:12
创建页面
创建动态表单 在菜单集合中添加四个按钮
使用SDK调用(账号密码) VVHD_tbButton 使用SDK调用(第三方登录)VVHD_tbButton_1
通过HTTP调用(账号密码)VVHD_tbButton_2 通过HTTP调用(第三方登录)VVHD_tbButton_3
页面中添加 多行文本框 F_VVHD_Remarks
请求参数
string dbId = "66691cXXXXXXXX";//数据中心ID string userName = "123";//账号 string password = "123456";//密码 string appID = "288158_R76r3ZgEVrBZ559F34XXXXXXXXXXXX";//应用ID string appSec = "23ac9d7f4c8d4779a7XXXXXXXXXXXX";//应用密钥 string URL = "http://127.0.0.1/K3Cloud/";//金蝶登录地址
使用SDK调用API
账号密码登录
K3CloudApi client = new K3CloudApi(URL); client.ValidateLogin(dbId, userName, password, 2052);
第三方登录授权登录
K3CloudApi client = new K3CloudApi(URL); client.LoginByAppSecret(dbId, userName, appID, appSec, 2052);
不使用SDK调用API
HTTP Post请求方法
public string Post(string url, string json) { var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; byte[] byteData = Encoding.UTF8.GetBytes(json); int length = byteData.Length; request.ContentLength = length; Stream writer = request.GetRequestStream(); writer.Write(byteData, 0, length); writer.Close(); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); return responseString.ToString(); }
账号密码登录
string url = URL + "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc"; string Json = "{\"acctID\":\"" + dbId + "\"," + "\"username\":\"" + userName + "\"," + "\"password\":\"" + password + "\"," + "\"lcid\":\"2052\"}"; Post(url, Json);
第三方登录授权登录
string guid = Guid.NewGuid().ToString();//Guid 全局唯一标识符 string timestamp = GetTimeStamp();//获取时间戳 string url = URL + "Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginByAppSecret.common.kdsvc"; string Json = "{\"format\": 1," + "\"useragent\": \"ApiClient\"," + "\"rid\": \"" + guid + "\"," + "\"parameters\": " + "\"[\\\"" + dbId + "\\\"," + "\\\"" + userName + "\\\"," + "\\\"" + appID + "\\\"," + "\\\"" + appSec + "\\\"," + "2052]\"," + "\"timestamp\": \"" + timestamp + "\"," + "\"v\": \"1.0\"}"; Post(url, Json);
请求示例
代码分享
using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Util; using Kingdee.BOS.WebApi.Client; using System; using System.ComponentModel; using System.IO; using System.Net; using System.Text; namespace XueXi { [Description("调用登录"), HotUpdate] public class 调用登录api : AbstractDynamicFormPlugIn { string dbId = "66691cXXXXXXXX";//数据中心ID string userName = "123";//账号 string password = "123456";//密码 string appID = "288158_R76r3ZgEVrBZ559F34XXXXXXXXXXXX";//应用ID string appSec = "23ac9d7f4c8d4779a7XXXXXXXXXXXX";//应用密钥 string URL = "http://127.0.0.1/K3Cloud/";//金蝶登录地址 public override void BarItemClick(BarItemClickEventArgs e) { base.BarItemClick(e); if (e.BarItemKey == "VVHD_tbButton") { K3CloudApi client = new K3CloudApi(URL); string ret = client.ValidateLogin(dbId, userName, password, 2052); this.Model.SetValue("F_VVHD_Remarks", ret); } else if (e.BarItemKey == "VVHD_tbButton_1") { K3CloudApi client = new K3CloudApi(URL); string ret = client.LoginByAppSecret(dbId, userName, appID, appSec, 2052); this.Model.SetValue("F_VVHD_Remarks", ret); } else if (e.BarItemKey == "VVHD_tbButton_2") { string url = URL + "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc"; string Json = "{\"acctID\":\"" + dbId + "\"," + "\"username\":\"" + userName + "\"," + "\"password\":\"" + password + "\"," + "\"lcid\":\"2052\"}"; string ret = Post(url, Json); this.Model.SetValue("F_VVHD_Remarks", ret); } else if (e.BarItemKey == "VVHD_tbButton_3") { string guid = Guid.NewGuid().ToString();//Guid 全局唯一标识符 string timestamp = GetTimeStamp(); string url = URL + "Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginByAppSecret.common.kdsvc"; string Json = "{\"format\": 1," + "\"useragent\": \"ApiClient\"," + "\"rid\": \"" + guid + "\"," + "\"parameters\": " + "\"[\\\"" + dbId + "\\\"," + "\\\"" + userName + "\\\"," + "\\\"" + appID + "\\\"," + "\\\"" + appSec + "\\\"," + "2052]\"," + "\"timestamp\": \"" + timestamp + "\"," + "\"v\": \"1.0\"}"; string ret = Post(url, Json); this.Model.SetValue("F_VVHD_Remarks", ret); } } /// <summary> /// 获取时间戳 /// </summary> /// <returns></returns> public string GetTimeStamp() { TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } /// <summary> /// HTTP POST请求 /// </summary> /// <param name="url">请求路径</param> /// <param name="json">Json报文</param> /// <returns></returns> public string Post(string url, string json) { var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; byte[] byteData = Encoding.UTF8.GetBytes(json); int length = byteData.Length; request.ContentLength = length; Stream writer = request.GetRequestStream(); writer.Write(byteData, 0, length); writer.Close(); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); return responseString.ToString(); } } }
赞 9
9人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读