oauth2.0协议接口-第一篇-api逻辑

开放平台是支持OAuth2.0和RESTful协议的资源分享平台,经过授权的合作伙伴可以读取和写入资讯、用户、文件、数据库等资源。

1.创建数据库表结构

CMSSyncClient(数据同步客户端)

CMSSyncGateway(数据同步网关)

CMSSyncAuth(数据同步授权)

CMSSyncSession(数据同步令牌)

CMSSyncLog(数据同步日志)

2.身份验证

接入BICloud开放平台的合作伙伴首先需要申请开发者账号,在开放平台>>身份验证模块中可以管理身份验证信息。

点击添加按钮后,按页面提示输入合作伙伴名称、开发者账号、开发者密钥即可。

2.应用服务

   

开放平台可供调用的API共40余项,其中每一项均对应一种资源,例如文章、政策、用户等。每项资源包括四种操作:

Read读取资源内容,公开、受限、私有权限可访问

Create创建新资源,受限、私有权限可访问

Update更新资源内容,私有权限可访问

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         }
原文地址:https://www.cnblogs.com/bwlluck/p/6664226.html