|
| 1 | +# 使用应用商店应用调用服务端API |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +- 如何获取app_access_token,请看:[获取app_access_token](https://open.feishu.cn/document/ukTMukTMukTM/uEjNz4SM2MjLxYzM) (应用商店应用) |
| 6 | + - 与企业自建应用相比,应用商店应用的获取app_access_token的流程复杂一些。 |
| 7 | + - 需要开放平台下发的app_ticket,通过订阅事件接收。SDK已经封装了app_ticket事件的处理,只需要启动事件订阅服务。 |
| 8 | + - 使用SDK调用服务端API时,如果当前还没有收到开发平台下发的app_ticket,会报错且向开放平台申请下发app_ticket,可以尽快的收到开发平台下发的app_ticket,保证再次调用服务端API的正常。 |
| 9 | + - 使用SDK调用服务端API时,需要使用tenant_access_token访问凭证时,需要 tenant_key ,来表示当前是哪个租户使用这个应用调用服务端API。 |
| 10 | + - tenant_key,租户安装启用了这个应用,开放平台发送的服务端事件,事件内容中都含有tenant_key。 |
| 11 | + |
| 12 | +### 使用`应用商店应用`访问 [发送文本消息API](https://open.feishu.cn/document/ukTMukTMukTM/uUjNz4SN2MjL1YzM) 示例 |
| 13 | + |
| 14 | +- 第一步:启动启动事件订阅服务,用于接收`app_ticket`。 |
| 15 | + |
| 16 | +```go |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/larksuite/oapi-sdk-go/core/config" |
| 21 | + "github.com/larksuite/oapi-sdk-go/core/constants" |
| 22 | + "github.com/larksuite/oapi-sdk-go/core/log" |
| 23 | + eventhttpserver "github.com/larksuite/oapi-sdk-go/event/http/native" |
| 24 | + "net/http" |
| 25 | +) |
| 26 | + |
| 27 | +var conf *config.Config |
| 28 | + |
| 29 | +func init() { |
| 30 | + // 企业自建应用的配置 |
| 31 | + // AppID、AppSecret: "开发者后台" -> "凭证与基础信息" -> 应用凭证(App ID、App Secret) |
| 32 | + // VerificationToken、EncryptKey:"开发者后台" -> "事件订阅" -> 事件订阅(Verification Token、Encrypt Key)。 |
| 33 | + appSetting := config.NewISVAppSettings("AppID", "AppSecret", "VerificationToken", "EncryptKey") |
| 34 | + |
| 35 | + // 当前访问的是飞书,config.NewConfig()的使用,请看:README.zh.md->高级使用->如何构建整体配置(Config) |
| 36 | + conf = config.NewConfig(constants.DomainFeiShu, appSetting, logger, log.LevelInfo, store) |
| 37 | +} |
| 38 | + |
| 39 | +func main() { |
| 40 | + // 启动httpServer,"开发者后台" -> "事件订阅" 请求网址 URL:https://domain/webhook/event |
| 41 | + eventhttpserver.Register("/webhook/event", conf) |
| 42 | + err := http.ListenAndServe(":8089", nil) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +- 第二步:调用服务端接口,有些老版接口,没有直接可以使用的SDK,可以使用`原生`模式。 |
| 50 | + |
| 51 | +```go |
| 52 | +package main |
| 53 | + |
| 54 | +import ( |
| 55 | + "context" |
| 56 | + "fmt" |
| 57 | + "github.com/larksuite/oapi-sdk-go/api" |
| 58 | + "github.com/larksuite/oapi-sdk-go/api/core/request" |
| 59 | + "github.com/larksuite/oapi-sdk-go/api/core/response" |
| 60 | + "github.com/larksuite/oapi-sdk-go/core" |
| 61 | + "github.com/larksuite/oapi-sdk-go/core/config" |
| 62 | + "github.com/larksuite/oapi-sdk-go/core/constants" |
| 63 | + "github.com/larksuite/oapi-sdk-go/core/log" |
| 64 | + "github.com/larksuite/oapi-sdk-go/core/tools" |
| 65 | +) |
| 66 | + |
| 67 | +func main() { |
| 68 | + // 应用商店应用的配置 |
| 69 | + // AppID、AppSecret: 开发者后台的应用凭证(App ID、App Secret) |
| 70 | + // VerificationToken、EncryptKey:开发者后台的事件订阅(Verification Token、Encrypt Key),可以为空字符串。 |
| 71 | + appSetting := config.NewISVAppSettings("AppID", "AppSecret", "VerificationToken", "EncryptKey") |
| 72 | + |
| 73 | + // 当前访问的是飞书,config.NewConfig()的使用,请看:README.zh.md->高级使用->如何构建整体配置(Config) |
| 74 | + conf := config.NewConfig(constants.DomainFeiShu, appSetting, logger, log.LevelInfo, store) |
| 75 | + |
| 76 | + // 发送消息的内容 |
| 77 | + body := map[string]interface{}{ |
| 78 | + "open_id": "user open id", |
| 79 | + "msg_type": "text", |
| 80 | + "content": map[string]interface{}{ |
| 81 | + "text": "test send message", |
| 82 | + }, |
| 83 | + } |
| 84 | + // 请求发送消息的结果 |
| 85 | + ret := make(map[string]interface{}) |
| 86 | + // 构建请求&&设置租户标识(tenant_key) |
| 87 | + req := request.NewRequestWithNative("message/v4/send", "POST", request.AccessTokenTypeTenant, |
| 88 | + body, &ret, request.SetTenantKey("tenant_key")) |
| 89 | + // 请求的上下文 |
| 90 | + coreCtx := core.WrapContext(context.Background()) |
| 91 | + // 发送请求 |
| 92 | + err := api.Send(coreCtx, conf, req) |
| 93 | + // 打印请求的RequestID |
| 94 | + fmt.Println(coreCtx.GetRequestID()) |
| 95 | + // 打印请求的响应状态吗 |
| 96 | + fmt.Println(coreCtx.GetHTTPStatusCode()) |
| 97 | + // 请求的error处理 |
| 98 | + if err != nil { |
| 99 | + e := err.(*response.Error) |
| 100 | + fmt.Println(e.Code) |
| 101 | + fmt.Println(e.Msg) |
| 102 | + fmt.Println(tools.Prettify(err)) |
| 103 | + return |
| 104 | + } |
| 105 | + // 打印请求的结果 |
| 106 | + fmt.Println(tools.Prettify(ret)) |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +## 使用`应用商店应用`访问 [修改用户部分信息API](https://open.feishu.cn/document/contact/v3/user/patch) 示例 |
| 111 | + |
| 112 | +- 第一步:略,同上 |
| 113 | + |
| 114 | +- 第二步:调用服务端接口,该接口是新的接口,可以直接使用SDK。 |
| 115 | + |
| 116 | +```go |
| 117 | +package main |
| 118 | + |
| 119 | +import ( |
| 120 | + "context" |
| 121 | + "fmt" |
| 122 | + "github.com/larksuite/oapi-sdk-go/api/core/request" |
| 123 | + "github.com/larksuite/oapi-sdk-go/api/core/response" |
| 124 | + "github.com/larksuite/oapi-sdk-go/core" |
| 125 | + "github.com/larksuite/oapi-sdk-go/core/config" |
| 126 | + "github.com/larksuite/oapi-sdk-go/core/constants" |
| 127 | + "github.com/larksuite/oapi-sdk-go/core/log" |
| 128 | + "github.com/larksuite/oapi-sdk-go/core/tools" |
| 129 | + contact "github.com/larksuite/oapi-sdk-go/service/contact/v3" |
| 130 | +) |
| 131 | + |
| 132 | +var conf *config.Config |
| 133 | + |
| 134 | +func init() { |
| 135 | + // 企业自建应用的配置 |
| 136 | + // AppID、AppSecret: "开发者后台" -> "凭证与基础信息" -> 应用凭证(App ID、App Secret) |
| 137 | + // VerificationToken、EncryptKey:"开发者后台" -> "事件订阅" -> 事件订阅(Verification Token、Encrypt Key)。 |
| 138 | + appSetting := config.NewISVAppSettings("AppID", "AppSecret", "VerificationToken", "EncryptKey") |
| 139 | + |
| 140 | + // 当前访问的是飞书,config.NewConfig()的使用,请看:README.zh.md->高级使用->如何构建整体配置(Config) |
| 141 | + conf = config.NewConfig(constants.DomainFeiShu, appSetting, logger, log.LevelInfo, store) |
| 142 | +} |
| 143 | + |
| 144 | +func main() { |
| 145 | + service := contact.NewService(conf) |
| 146 | + coreCtx := core.WrapContext(context.Background()) |
| 147 | + body := &contact.User{} |
| 148 | + body.Name = "rename" |
| 149 | + // 由于这是一个PATCH请求,需要告之更新哪些字段 |
| 150 | + body.ForceSendFields = append(body.ForceSendFields, "Name") |
| 151 | + // 构建请求&&设置租户标识(tenant_key) |
| 152 | + reqCall := service.Users.Patch(coreCtx, body, request.SetTenantKey("tenant_key")) |
| 153 | + reqCall.SetUserId("user id") |
| 154 | + reqCall.SetUserIdType("user_id") |
| 155 | + // 发送请求 |
| 156 | + result, err := reqCall.Do() |
| 157 | + // 打印请求的RequestID |
| 158 | + fmt.Println(coreCtx.GetRequestID()) |
| 159 | + // 打印请求的响应状态吗 |
| 160 | + fmt.Println(coreCtx.GetHTTPStatusCode()) |
| 161 | + // 请求的error处理 |
| 162 | + if err != nil { |
| 163 | + e := err.(*response.Error) |
| 164 | + fmt.Println(e.Code) |
| 165 | + fmt.Println(e.Msg) |
| 166 | + fmt.Println(tools.Prettify(err)) |
| 167 | + return |
| 168 | + } |
| 169 | + // 打印请求的结果 |
| 170 | + fmt.Println(tools.Prettify(result)) |
| 171 | +} |
| 172 | +``` |
0 commit comments