在《C#開發BIMFACE系列3 服務端API之獲取應用訪問憑證AccessToken》中詳細介紹了應用程序訪問API的令牌憑證。咱們知道 Access token 表明自身應用的身份,使用應用的 appkey, secret,經過調用/oauth2/token接口獲取。BIMFACE全部的接口調用都須要傳遞 Access token 。html
本篇主要介紹 ViewToken。前端
表明對單個模型/集成模型/模型對比的訪問權限,使用 access token,經過調用/view/token或其餘相關接口得到。api
使用 Access token,能夠對本身應用內的文件發起文件上傳,下載,刪除,模型轉換,模型集成,模型對比等操做, 同時也能訪問全部 BIMFACE 的數據接口獲取轉換後的模型BIM信息;而 View token 只表明對單個模型/集成模型/模型對比的臨時的訪問憑證, 只能訪問對應模型的數據接口,經過使用應用的 Access token 調用下面的接口能夠得到。 一般狀況下,View token 能夠直接傳入前端 JSSDK 用來加載/瀏覽模型。app
View token的使用方法是在調用對應的數據接口的時候,添加一個查詢參數(Query parameter):ide
view_token={your_view_token} 函數
只有在文件轉換或模型集成任務成功之後,才能獲取View token。測試
請求地址:GET https://api.bimface.com/view/tokenthis
說明:經過fileId, integrateId, compareId 獲取View token, 而後把View token傳入前端JavaScript組件提供的接口中,便可加載和瀏覽文件所包含的三維模型或二維圖紙。url
參數:application/octet-streamspa
請求 path(示例):https://api.bimface.com/view/token
請求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"
HTTP響應示例(200):
{ "code": "success", "message": null, "data": "389c28de59ee62e66a7d87ec12692a76" }
失敗時返回:
{ "code": "authentication.failed", "message": "Token was not recognized." }
失敗時返回的錯誤碼:
C#實現方法:
1 /// <summary> 2 /// 獲取模型的 ViewToken 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="modelType">模型類型</param> 6 /// <param name="objectId">文件轉換ID(fileId)、模型對比ID(compareId)、集成模型ID(integrateId)的值,三者中的一個</param> 7 /// <returns></returns> 8 protected ViewTokenResponse GetViewToken(string accessToken, ModelType modelType, long objectId) 9 { 10 //GET https://api.bimface.com/view/token 11 string url = string.Format(BimfaceConstants.API_HOST + "/view/token?{0}={1}", modelType.ToString(), objectId); 12 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 13 headers.AddOAuth2Header(accessToken); 14 15 try 16 { 17 ViewTokenResponse response; 18 HttpManager httpManager = new HttpManager(headers); 19 HttpResult httpResult = httpManager.Get(url); 20 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 21 { 22 response = httpResult.Text.DeserializeJsonToObject<ViewTokenResponse>(); 23 } 24 else 25 { 26 response = new ViewTokenResponse 27 { 28 Message = httpResult.RefText 29 }; 30 } 31 32 return response; 33 } 34 catch (Exception ex) 35 { 36 throw new Exception("[獲取ViewToken] 發生異常!", ex); 37 } 38 }
其中調用到的 httpManager.Get() 方法,請參考《C# HTTP系列》
其中 ModelType 枚舉,是爲了區分不一樣模型的種類
1 /// <summary> 2 /// 模型類型枚舉 3 /// </summary> 4 public enum ModelType 5 { 6 /// <summary> 7 /// 文件轉換ID 8 /// </summary> 9 fileId, 10 11 /// <summary> 12 /// 模型對比ID 13 /// </summary> 14 compareId, 15 16 /// <summary> 17 /// 集成模型ID 18 /// </summary> 19 integrateId 20 }
爲了使調用更加的方便,擴展了3個更細緻的方法
1 /// <summary> 2 /// 獲取單個模型的 ViewToken 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="fileId">文件轉換ID</param> 6 /// <returns></returns> 7 public ViewTokenResponse GetViewTokenByFileId(string accessToken, long fileId) 8 { 9 return GetViewToken(accessToken, ModelType.fileId, fileId); 10 }
1 /// <summary> 2 /// 獲取模型集成的 ViewToken 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="integrateId">集成模型ID</param> 6 /// <returns></returns> 7 public ViewTokenResponse GetViewTokenByIntegrateId(string accessToken, long integrateId) 8 { 9 return GetViewToken(accessToken, ModelType.integrateId, integrateId); 10 }
1 /// <summary> 2 /// 獲取模型比對的 ViewToken 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="compareId">模型比對ID</param> 6 /// <returns></returns> 7 public ViewTokenResponse GetViewTokenByCompareId(string accessToken, long compareId) 8 { 9 return GetViewToken(accessToken, ModelType.compareId, compareId); 10 }
在BIMFACE的控制檯中能夠看到咱們上傳的文件列表,共計2個文件。模型狀態均爲轉換成功。
【獲取ViewToken】、【公開連接】按鈕只有在模型轉換成功以後才啓用。
調用上面封裝的方法來測試是否能獲取到viewToken,以第一個文件「rac_advanced_sample_project-三維視圖 - From Parking Area.dwg」 爲例
在BIMFACE控制檯中查看該文件的ViewToken
測試程序以下:
1 // 獲取 ViewToken【文件轉換ID】 2 protected void btnGetViewTokenByFileId_Click(object sender, EventArgs e) 3 { 4 BasicApi api = new BasicApi(); 5 ViewTokenResponse response = api.GetViewTokenByFileId(txtAccessToken.Text, txtFileId.Text.ToLong()); 6 7 txtResult.Text = response.Code 8 + Environment.NewLine 9 + response.Message 10 + Environment.NewLine 11 + response.Data.ToString2(); 12 }
返回的結果對應的實體類以下:
1 /// <summary> 2 /// 獲取 ViewToken 的請求返回結果類 3 /// </summary> 4 [Serializable] 5 public class ViewTokenResponse : GeneralResponse<string> 6 { 7 8 }
繼承的基類以下:
1 /// <summary> 2 /// 請求 BIMFACE 服務端 API的響應結果統一的返回類 3 /// </summary> 4 [Serializable] 5 public class GeneralResponse<T> //where T : class, new() 6 { 7 #region 屬性 8 9 /// <summary> 10 /// 請求返回代碼,success 或者 xxxx.failed。 11 /// </summary> 12 [JsonProperty("code")] 13 public virtual string Code { get; set; } 14 15 /// <summary> 16 /// 失敗的錯誤緣由。 17 /// 若是 Code 爲 success 則 Message 爲空。 18 /// 若是 Code 爲 xxxx.failed 則 Message 爲具體的失敗信息。 19 /// </summary> 20 [JsonProperty("message")] 21 public virtual string Message { get; set; } 22 23 /// <summary> 24 /// 執行成功後的返回結果 25 /// </summary> 26 [JsonProperty("data")] 27 public virtual T Data { get; set; } 28 29 #endregion 30 31 #region 構造函數 32 public GeneralResponse() 33 { 34 } 35 36 public GeneralResponse(T data) 37 { 38 this.Data = data; 39 } 40 41 #endregion 42 43 #region 方法 44 public override string ToString() 45 { 46 if (Data != null) 47 { 48 return string.Format("GeneralResponse [code={0}, message={1}, data={2}]", Code, Message, Data); 49 } 50 else 51 { 52 return string.Format("GeneralResponse [code={0}, message={1}, data={2}]", Code, Message, ""); 53 } 54 } 55 56 #endregion 57 }