安全与认证

为了使用Interact API,您必须是经过验证的用户。API将遵守通过网站分配给用户的任何权限,这意味着用户将只能通过他们能够访问的API访问内容。

只有在您的交互实例之外使用API时,才需要通过令牌系统进行直接身份验证。Interact提供了在自由文本小部件或MasterPage javascript功能中输入自定义javascript的能力。如果使用来自小部件或开发人员框架的API,则从登录到交互内部网的用户获取身份验证,因此不需要显式身份验证。

如果通过您的Interact实例之外的环境访问API,则需要身份验证。要根据API进行身份验证,请使用/令牌端点,并传递用户的用户名和密码(注意:此端点不包括/ api /路径的一部分)。如果用户成功通过身份验证,则此端点将在其响应中返回访问令牌。然后需要在每个请求的授权头中提交此访问令牌。

下面显示的示例c#代码验证了一个用户,然后请求一个人员列表,然后将每个人的Id和Name写入控制台。

static void Main(string[] args) {var tenant = new Guid("e822a190-a116-4779-ad0a-87631b51898a");var apiddomain = "{{your_apiddomain}}";Var username = "w.skroob";Var password = "12345";var accessToken = "";// do login var body = new Dictionary();的身体。添加(“grant_type”、“密码”);的身体。添加(“用户名”,用户名); body.Add("password", password); var resp = Post($"{apiDomain}/token", body, tenant); dynamic obj = JsonConvert.DeserializeObject(resp); accessToken = obj.access_token; refreshToken = obj.refresh_token; dynamic people = JsonConvert.DeserializeObject(Get($"{apiDomain}/api/people", tenant, accessToken)); foreach (dynamic person in people.Results) { Console.WriteLine($"{person.personId} - {person.FullName}"); } } static string Post(string url, Dictionary form, Guid tenant) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Headers.Add("X-Tenant", tenant.ToString()); request.Method = "POST"; string body = QueryString(form); System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); Byte[] byteArray = encoding.GetBytes(body); request.ContentLength = byteArray.Length; request.ContentType = @"application/x-www-form-urlencoded"; using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); return reader.ReadToEnd(); } } catch (WebException ex) { WebResponse errorResponse = ex.Response; using (Stream responseStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); string errorText = reader.ReadToEnd(); } throw; } } static string Get(string url, Guid tenant, string accessToken) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Headers.Add("X-Tenant", tenant.ToString()); request.Headers.Add("Authorization", $"Bearer {accessToken}"); try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); return reader.ReadToEnd(); } } catch (WebException ex) { WebResponse errorResponse = ex.Response; using (Stream responseStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); string errorText = reader.ReadToEnd(); } throw; } } static byte[] GetBytes(string url, Guid tenant, string accessToken) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Headers.Add("X-Tenant", tenant.ToString()); request.Headers.Add("Authorization", $"Bearer {accessToken}"); try { WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { using (MemoryStream ms = new MemoryStream()) { responseStream.CopyTo(ms); return ms.ToArray(); } } } catch (WebException ex) { WebResponse errorResponse = ex.Response; using (Stream responseStream = errorResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); string errorText = reader.ReadToEnd(); } throw; } } public static string QueryString(IDictionary dict) { var list = new List(); foreach (var item in dict) { list.Add(item.Key + "=" + item.Value); } return string.Join("&", list); }
Baidu
map