系列目錄???? 【已更新最新開發(fā)文章,點擊查看詳細(xì)】
<https://www.cnblogs.com/SavionZhang/p/11424431.html>
BIMFACE 平臺為開發(fā)者提供了大量的服務(wù)器端 API 與 JavaScript API,用于二次開發(fā) BIM 的相關(guān)應(yīng)用。
BIMFACE 所有的 RESTful API 都有對應(yīng)的鑒權(quán)機(jī)制保護(hù),目前 BIMFACE 支持兩種鑒權(quán)方式:
Access token
代表自身應(yīng)用的身份,使用應(yīng)用的 appkey, secret,通過調(diào)用/oauth2/token接口獲取。
View token
代表對單個模型/集成模型/模型對比的訪問權(quán)限,使用 access token,通過調(diào)用/view/token或其他相關(guān)接口獲得。
使用 Access token,可以對自己應(yīng)用內(nèi)的文件發(fā)起文件上傳,下載,刪除,模型轉(zhuǎn)換,模型集成,模型對比等操作, 同時也能訪問所有 BIMFACE
的數(shù)據(jù)接口獲取轉(zhuǎn)換后的模型BIM信息;而 View token 只代表對單個模型/集成模型/模型對比的臨時的訪問憑證,
只能訪問對應(yīng)模型的數(shù)據(jù)接口,通過使用應(yīng)用的 Access token 調(diào)用下面的接口可以獲得。 通常情況下,View token 可以直接傳入前端 JSSDK
用來加載/瀏覽模型。
Access token 有效期為7天, 除非 token 被注銷,Access token 在7天內(nèi)不會發(fā)生改變; 而 View token
只是一個臨時的訪問憑證,有效期為12小時。但是為了減少用戶重復(fù)請求 View token 的次數(shù), 每次使用 View token
都會重置有效期為12小時。這樣如果你的模型持續(xù)有人訪問,View token 會一直有效, 只有在12小時內(nèi),沒有使用 View token
的任何調(diào)用,View token 才會失效。
Access token 只能使用 appkey, secret 通過/oauth2/token接口獲取; 類似的,View token 必須通過有效的
Access token 并提供對應(yīng)的源文件Id以及集成模型Id信息來獲取。
關(guān)于請求中的 Header Authorization 的使用
獲取 Access token 接口中使用的 Authorization,是將字符串 appKey:appSecret
拼接后(中間用冒號連接),對其進(jìn)行BASE64編碼, 然后在編碼后的字符串前添加字符串Basic和一個空格, 即:“Basic
[Base64Encode(“appKey:appSecret”)]“。
其他接口中使用的 Header Authorization, 是將你的 Access token 的字符串前添加字符串bearer和一個空格,
即:“bearer [access token]" 。
?
BASE64編碼與解碼的方法:
/// <summary> /// 使用 UTF8 編碼格式,對字符串進(jìn)行進(jìn)行 Base64 方式編碼(加密) /// </summary> ///
<param name="this">擴(kuò)展對象</param> /// <returns>編碼后的字符串</returns> public static
string EncryptByBase64(this string @this) { byte[] bytes =
Encoding.UTF8.GetBytes(@this);return Convert.ToBase64String(bytes); } ///
<summary> /// 使用 UTF8 編碼格式,對字符串進(jìn)行進(jìn)行 Base64 方式解碼(解密) /// </summary> /// <param
name="this">擴(kuò)展對象</param> /// <returns>解碼后的字符串</returns> public static string
DecryptByBase64(this string @this) { byte[] bytes =
Convert.FromBase64String(@this);return Encoding.UTF8.GetString(bytes); } ? 獲取
AccessToken 請求地址:POST https://api.bimface.com/oauth2/token 說明:
在調(diào)用其他API之前,必須先獲取Access Token。Access Token的有效期為7天。
參數(shù):
獲取AccessToken的方法:
1 /// <summary> 2 /// 獲取訪問服務(wù)端其他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.RefText29 }; 30 } 31 32 return response; 33 } 34 catch (Exception
ex)35 { 36 throw new Exception("獲取 AccessToken 時發(fā)生異常!", ex); 37 } 38 }
?在網(wǎng)頁中測試上面的方法:
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 }
在監(jiān)視窗口中可以看到,接口調(diào)用返回了正確的結(jié)果:
在調(diào)試窗口中也可以看到正確的響應(yīng)結(jié)果:
上述方法中調(diào)用到的 httpManger.Post(url)方法?
1 /// <summary> 2 /// HTTP-POST方法,(不包含body數(shù)據(jù))。 3 /// 發(fā)送 HTTP 請求并返回來自
Internet 資源的響應(yīng)(HTML代碼) 4 /// </summary> 5 /// <param name="url">請求目標(biāo)URL</param>
6 /// <returns>HTTP-POST的響應(yīng)結(jié)果</returns> 7 public HttpResult Post(string url)
8 { 9 return RequestString(url, null, WebRequestMethods.Http.Post, null); 10 }
1 /// <summary> 2 /// HTTP請求(包含文本的body數(shù)據(jù)) 3 /// </summary> 4 /// <param
name="url">請求目標(biāo)URL</param> 5 /// <param name="data">
主體數(shù)據(jù)(普通文本或者JSON文本)。如果參數(shù)中有中文,請使用合適的編碼方式進(jìn)行編碼,例如:gb2312或者utf-8</param> 6 ///
<param name="method">請求的方法。請使用 WebRequestMethods.Http 的枚舉值</param> 7 ///
<param name="contentType"><see langword="Content-type" /> HTTP 標(biāo)頭的值。請使用
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中。如果設(shè)置了WebHeaderCollection,則屬性值將丟失。所以放置在Headers
屬性之后設(shè)置 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);//將請求參數(shù)寫入請求流中 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 }
?
系列目錄???? 【已更新最新開發(fā)文章,點擊查看詳細(xì)】
<https://www.cnblogs.com/SavionZhang/p/11424431.html>
熱門工具 換一換