BIMFACE 平臺爲開發者提供了大量的服務器端 API 與 JavaScript API,用於二次開發 BIM 的相關應用。html
BIMFACE 全部的 RESTful API 都有對應的鑑權機制保護,目前 BIMFACE 支持兩種鑑權方式:前端
表明自身應用的身份,使用應用的 appkey, secret,經過調用/oauth2/token接口獲取。web
表明對單個模型/集成模型/模型對比的訪問權限,使用 access token,經過調用/view/token或其餘相關接口得到。api
使用 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 纔會失效。 app
Access token 只能使用 appkey, secret 經過/oauth2/token接口獲取; 相似的,View token 必須經過有效的 Access token 並提供對應的源文件Id以及集成模型Id信息來獲取。 post
獲取 Access token 接口中使用的 Authorization,是將字符串 appKey:appSecret 拼接後(中間用冒號鏈接),對其進行BASE64編碼, 而後在編碼後的字符串前添加字符串Basic和一個空格, 即:「Basic [Base64Encode(「appKey:appSecret」)]「。測試
其餘接口中使用的 Header Authorization, 是將你的 Access token 的字符串前添加字符串bearer和一個空格, this
即:「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天。
參數:
獲取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 //POST https://api.bimface.com/oauth2/token 10 string url = BimfaceConstants.API_HOST + "/oauth2/token"; 11 12 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 13 headers.AddBasicAuthHeader(appKey, appSecret); 14 15 try 16 { 17 AccessTokenResponse response; 18 HttpManager httpManager = new HttpManager(headers); 19 HttpResult httpResult = httpManager.Post(url); 20 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 21 { 22 response = httpResult.Text.DeserializeJsonToObject<AccessTokenResponse>(); 23 } 24 else 25 { 26 response = new AccessTokenResponse 27 { 28 Message = httpResult.RefText 29 }; 30 } 31 32 return response; 33 } 34 catch (Exception ex) 35 { 36 throw new Exception("獲取 AccessToken 時發生異常!", ex); 37 } 38 }
在網頁中測試上面的方法:
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 }
在監視窗口中能夠看到,接口調用返回了正確的結果:
在調試窗口中也能夠看到正確的響應結果:
上述方法中調用到的 httpManger.Post(url)方法
1 /// <summary> 2 /// HTTP-POST方法,(不包含body數據)。 3 /// 發送 HTTP 請求並返回來自 Internet 資源的響應(HTML代碼) 4 /// </summary> 5 /// <param name="url">請求目標URL</param> 6 /// <returns>HTTP-POST的響應結果</returns> 7 public HttpResult Post(string url) 8 { 9 return RequestString(url, null, WebRequestMethods.Http.Post, null); 10 }
1 /// <summary> 2 /// HTTP請求(包含文本的body數據) 3 /// </summary> 4 /// <param name="url">請求目標URL</param> 5 /// <param name="data">主體數據(普通文本或者JSON文本)。若是參數中有中文,請使用合適的編碼方式進行編碼,例如:gb2312或者utf-8</param> 6 /// <param name="method">請求的方法。請使用 WebRequestMethods.Http 的枚舉值</param> 7 /// <param name="contentType"><see langword="Content-type" /> HTTP 標頭的值。請使用 ContentType 類的常量來獲取</param> 8 /// <returns></returns> 9 private HttpResult RequestString(string url, string data, string method, string contentType) 10 { 11 HttpResult httpResult = new HttpResult(); 12 HttpWebRequest httpWebRequest = null; 13 14 try 15 { 16 httpWebRequest = WebRequest.Create(url) as HttpWebRequest; 17 httpWebRequest.Method = method; 18 httpWebRequest.Headers = HeaderCollection; 19 httpWebRequest.CookieContainer = CookieContainer; 20 if (!string.IsNullOrWhiteSpace(contentType)) 21 { 22 httpWebRequest.ContentType = contentType;// 此屬性的值存儲在WebHeaderCollection中。若是設置了WebHeaderCollection,則屬性值將丟失。因此放置在Headers 屬性以後設置 23 } 24 httpWebRequest.UserAgent = _userAgent; 25 httpWebRequest.AllowAutoRedirect = _allowAutoRedirect; 26 httpWebRequest.ServicePoint.Expect100Continue = false; 27 28 if (data != null) 29 { 30 httpWebRequest.AllowWriteStreamBuffering = true; 31 using (Stream requestStream = httpWebRequest.GetRequestStream()) 32 { 33 requestStream.Write(EncodingType.GetBytes(data), 0, data.Length);//將請求參數寫入請求流中 34 requestStream.Flush(); 35 } 36 } 37 38 HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; 39 if (httpWebResponse != null) 40 { 41 GetResponse(ref httpResult, httpWebResponse); 42 httpWebResponse.Close(); 43 } 44 } 45 catch (WebException webException) 46 { 47 GetWebExceptionResponse(ref httpResult, webException); 48 } 49 catch (Exception ex) 50 { 51 GetExceptionResponse(ref httpResult, ex, method, contentType); 52 } 53 finally 54 { 55 if (httpWebRequest != null) 56 { 57 httpWebRequest.Abort(); 58 } 59 } 60 61 return httpResult; 62 }