系列目录     【已更新最新开发文章,点击查看详细】
<https://www.cnblogs.com/SavionZhang/p/11424431.html>
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信息来获取。
关于请求中的 Header Authorization 的使用
获取 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); }   获取
AccessToken 请求地址: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.RefText29 }; 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 }
 
系列目录     【已更新最新开发文章,点击查看详细】
<https://www.cnblogs.com/SavionZhang/p/11424431.html>

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信