BIMFACE 平台为开发者提供了大量的服务器端 API 与 JavaScript API,用于二次开发 BIM 的相关应用。
BIMFACE 所有的 RESTful API 都有对应的鉴权机制保护,目前 BIMFACE 支持两种鉴权方式:
- Access token
-
代表自身应用的身份,使用应用的 appkey, secret,通过调用/oauth2/token接口获取。
- View token
-
代表对单个模型/集成模型/模型对比的访问权限,使用 access token,通过调用/view/token或其他相关接口获得。
使用 Access token,可以对自己应用内的文件发起文件上传,下载,删除,模型转换,模型集成,模型对比等操作, 同时也能访问所有 BIMFACE 的数据接口获取转换后的模型BIM信息;而 View token 只代表对单个模型/集成模型/模型对比的临时的访问凭证, 只能访问对应模型的数据接口,通过使用应用的 Access token调用下面的接口可以获得。 通常情况下,View token 可以直接传入前端 JSSDK 用来加载/浏览模型。
Access token 有效期为7天, 除非 token 被注销,Access token 在7天内不会发生改变; 而 View token 只是一个临时的访问凭证,有效期为12小时。但是为了减少用户重复请求 View token 的次数, 每次使用 View token 都会重置有效期为12小时。这样如果你的模型持续有人访问,View token 会一直有效, 只有在12小时内,没有使用 View token 的任何调用,View token才会失效。
Access token 只能使用 appkey, secret 通过/oauth2/token接口获取; 类似的,View token 必须通过有效的 Access token 并提供对应的源文件Id以及集成模型Id信息来获取。
获取 Access token 接口中使用的 Authorization,是将字符串 appKey:appSecret 拼接后(中间用冒号连接),对其进行BASE64编码, 然后在编码后的字符串前添加字符串Basic和一个空格, 即:“Basic [Base64Encode(“appKey:appSecret”)]“。
其他接口中使用的 header Authorization, 是将你的 Access token 的字符串前添加字符串bearer和一个空格,
即:“bearer [access token]" 。
BASE64编码与解码的方法:
/// <summary> /// 使用 UTF8 编码格式,对字符串进行进行 Base64 方式编码(加密) /// </summary> /// <param name="this">扩展对象</param> /// <returns>编码后的字符串</returns> public static string EncryptByBase64(this string @this) { byte[] bytes = Encoding.UTF8.GetBytes(@this); return Convert.ToBase64String(bytes); }
/// <summary> /// 使用 UTF8 编码格式,对字符串进行进行 Base64 方式解码(解密) /// </summary> /// <param name="this">扩展对象</param> /// <returns>解码后的字符串</returns> public static string DecryptByBase64(this string @this) { byte[] bytes = Convert.FromBase64String(@this); return Encoding.UTF8.GetString(bytes); }
请求地址:POST https://api.bimface.com/oauth2/token
说明:在调用其他API之前,必须先获取Access Token。Access Token的有效期为7天。
参数:
Web请求的通用方法:
1 /// <summary> 2 /// 向 BIMFACE 服务器发送 API 接口请求的工具类 3 /// </summary> 4 public class BimFaceRequestUtility 5 { 6 /// <summary> 7 /// (POST方式)发送请求到BimFace服务器并将返回结果(Json字符串)转换到具体的对象 8 /// </summary> 9 /// <param name="url">BimFace接口地址</param> 10 /// <param name="headerCollection">请求的头部信息</param> 11 /// <param name="requestParams">请求的参数(经过序列化之后的Json格式字符串)</param> 12 /// <returns></returns> 13 public static T SendPostRequest<T>(string url, WebHeaderCollection headerCollection, string requestParams = null) 14 { 15 return SendRequest<T>(url, HttpWebRequestMethod.POST, headerCollection, requestParams); 16 } 17 18 /// <summary> 19 /// (GET方式)发送请求到BimFace服务器并将返回结果(Json字符串)转换到具体的对象 20 /// </summary> 21 /// <param name="url">BimFace接口地址</param> 22 /// <param name="headerCollection">请求的头部信息</param> 23 /// <param name="requestParams">请求的参数(经过序列化之后的Json格式字符串)</param> 24 /// <returns></returns> 25 public static T SendGetRequest<T>(string url, WebHeaderCollection headerCollection, string requestParams = null) 26 { 27 return SendRequest<T>(url, HttpWebRequestMethod.GET, headerCollection, requestParams); 28 } 29 30 /// <summary> 31 /// 发送请求到BimFace服务器并将返回结果(Json字符串)转换到具体的对象 32 /// </summary> 33 /// <param name="url">BimFace接口地址</param> 34 /// <param name="method">发送请求的方式</param> 35 /// <param name="headerCollection">请求的头部信息</param> 36 /// <param name="requestParams">请求的参数(经过序列化之后的Json格式字符串)</param> 37 /// <returns></returns> 38 private static T SendRequest<T>(string url, HttpWebRequestMethod method, WebHeaderCollection headerCollection, string requestParams = null) 39 { 40 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 41 request.Method = method.ToString().ToUpper(); 42 request.ContentType = "application/json"; 43 request.Headers = headerCollection; 44 45 if (requestParams.IsNotNullAndWhiteSpace()) 46 { 47 byte[] byteRequest = requestParams.ToByteArrayByUTF8(); 48 request.ContentLength = byteRequest.Length; 49 50 Stream requestStream = request.GetRequestStream(); 51 requestStream.Write(byteRequest, 0, byteRequest.Length); 52 requestStream.Close(); 53 } 54 55 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 56 Stream responseStream = response.GetResponseStream(); 57 StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 58 string html = streamReader.ReadToEnd(); 59 T result = JsonConvert.DeserializeObject<T>(html); 60 61 return result; 62 } 63 }
获取AccessToken的方法:
1 /// <summary> 2 /// 获取访问服务端其他API的令牌 3 /// </summary> 4 /// <param name="appKey">秘钥</param> 5 /// <param name="appSecret">密码</param> 6 /// <returns></returns> 7 public AccessTokenResponse GetAccessToken(string appKey, string appSecret) 8 { 9 try 10 { 11 string url = "https://api.bimface.com/oauth2/token"; 12 string keyAndSecret = appKey + ":" + appSecret; 13 string authorizationParams = "Basic" + " " + keyAndSecret.EncryptByBase64();//Base64 编码 14 WebHeaderCollection headerCollection = new WebHeaderCollection 15 { 16 { "Authorization", authorizationParams } 17 }; 18 AccessTokenResponse response = BimFaceRequestUtility.SendPostRequest<AccessTokenResponse>(url, headerCollection); 19 20 return response; 21 } 22 catch (Exception ex) 23 { 24 throw new Exception("获取 AccessToken 时发生异常!", ex); 25 } 26 }
在网页中测试上面的方法:
1 /// <summary> 2 /// 获取 AccessToken 3 /// </summary> 4 protected void btnGetAccessToken_Click(object sender, EventArgs e) 5 { 6 string token = string.Empty; 7 string appKey = ConfigUtility.GetAppSettingValue("BIMFACE_AppKey"); 8 string appSecret = ConfigUtility.GetAppSettingValue("BIMFACE_AppSecret"); 9 10 IBasicApi api = new BasicApi(); 11 AccessTokenResponse response = api.GetAccessToken(appKey, appSecret); 12 if (response != null) 13 { 14 token = response.Data.Token; 15 } 16 }
在监视窗口中可以看到,接口调用返回了正确的结果