開放平台是支持OAuth2.0和RESTful協議的資源分享平台,經過授權的合作伙伴可以讀取和寫入資訊、用戶、文件、數據庫等資源。
1.創建數據庫表結構
CMSSyncClient(數據同步客戶端)
CMSSyncGateway(數據同步網關)
CMSSyncAuth(數據同步授權)
CMSSyncSession(數據同步令牌)
CMSSyncLog(數據同步日志)
2.身份驗證
接入BICloud開放平台的合作伙伴首先需要申請開發者賬號,在開放平台>>身份驗證模塊中可以管理身份驗證信息。
點擊添加按鈕后,按頁面提示輸入合作伙伴名稱、開發者賬號、開發者密鑰即可。
2.應用服務
開放平台可供調用的API共40余項,其中每一項均對應一種資源,例如文章、政策、用戶等。每項資源包括四種操作:
l Read:讀取資源內容,公開、受限、私有權限可訪問
l Create:創建新資源,受限、私有權限可訪問
l Update:更新資源內容,私有權限可訪問
l Delete:刪除現有資源,私有權限可訪問
點擊添加按鈕可以為指定的開發者賬號開通資源訪問權限,未授權的API默認訪問權限為公開,已授權的API默認訪問權限為受限,已授權並擁有用戶令牌的API默認訪問權限為私有。
按頁面提示選擇需要開通的API,然后點擊確認按鈕。
3.api請求類基類編寫
1 /// <summary> 2 /// 驗證並處理請求 3 /// </summary> 4 /// <param name="context"></param> 5 public void ProcessRequest(HttpContext context) 6 { 7 context.Response.ContentType = "application/json"; 8 context.Response.Charset = "utf-8"; 9 //獲得用戶身份 10 string getwayUrl = Path.GetFileNameWithoutExtension(new Uri(ALHttpIO.ResolveFullUrl(ALHttpIO.RawUrl)).LocalPath); 11 string apiKey = context.Request.Headers["ApiKey"] ?? context.Request.QueryString["ApiKey"]; //用戶標識13 //獲得請求信息 14 string content = this.getRequestContent(context); 15 this.Request = CMSSyncRequest.Parse(context.Request.QueryString["format"], (context.Request.Form.Count == 0) ? content : ""); 16 if (this.Request.Format == null) 17 { 18 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "不支持的傳輸格式")); 19 return; 20 } 21 this.Ident = CMSSyncAuthBO.GetToken(getwayUrl, apiKey, this.Request.ActionCode); 22 if (!Ident.Verify(content, securityToken) || !CMSSyncClient.API_Enable) 23 { 24 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "安全令牌校驗失敗")); 25 return; 26 } 27 try 28 { 29 //獲得請求參數 30 CMSSyncEventArgs ev = new CMSSyncEventArgs(this.Request); 31 switch (this.Request.ActionCode) 32 { 33 case "Create": 34 if (this.Create != null) 35 this.Create(this, ev); 36 else 37 { 38 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "非法操作:服務器尚未定義" + this.Request.ActionCode + "操作的響應")); 39 return; 40 } 41 break; 42 case "Update": 43 if (this.Update != null) 44 this.Update(this, ev); 45 else 46 { 47 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "非法操作:服務器尚未定義" + this.Request.ActionCode + "操作的響應")); 48 return; 49 } 50 break; 51 case "Delete": 52 if (this.Delete != null) 53 this.Delete(this, ev); 54 else 55 { 56 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "非法操作:服務器尚未定義" + this.Request.ActionCode + "操作的響應")); 57 return; 58 } 59 break; 60 case "Read": 61 if (this.Read != null) 62 this.Read(this, ev); 63 else 64 { 65 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "非法操作:服務器尚未定義" + this.Request.ActionCode + "操作的響應")); 66 return; 67 } 68 break; 69 default: 70 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "未知指令:" + this.Request.ActionCode)); 71 return; 72 } 73 this.EndResponse(context, ev.Response); 74 return; 75 } 76 catch (Exception ex) 77 { 78 this.EndResponse(context, CMSSyncResponse.Create(this.Request, EnumCMSSyncResponseCode.Failed, "運行時異常:" + ex.Message)); 79 return; 80 } 81 }