microapp

package module
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

字节跳动小程序golang sdk

快速开始&demo

// 创建字节小程序实例
app := microapp.New(microapp.Config{
AppId:     "APPID",
AppSecret: "SECRET",
})
登录
res, err := auth.Code2SessionV2(app, "anonymous_code", "code")
if err != nil {
    return nil, err
}

部分内容借鉴自:github.com/fastwego/microapp

Documentation

Overview

字节小程序开发 SDK

See: https://microapp.bytedance.com/

Index

Constants

This section is empty.

Variables

View Source
var (
	ServerUrl              = "https://developer.toutiao.com" //  api 服务器地址
	UserAgent              = "21888/microapp"
	ErrorAccessTokenExpire = errors.New("access token expire")
	ErrorSystemBusy        = errors.New("system busy")
)

Functions

func GetAccessToken

func GetAccessToken(ctx *MicroApp) (accessToken string, err error)

从 公众号实例 的 AccessToken 管理器 获取 access_token

如果没有 access_token 或者 已过期,那么刷新

获得新的 access_token 后 过期时间设置为 0.9 * expiresIn 提供一定冗余

func NoticeAccessTokenExpire

func NoticeAccessTokenExpire(ctx *MicroApp) (err error)

NoticeAccessTokenExpire 只需将本地存储的 access_token 删除,即完成了 access_token 已过期的 主动通知

retry 请求的时候,会发现本地没有 access_token ,从而触发refresh

Types

type Client

type Client struct {
	Ctx *MicroApp
}

HttpClient 用于向接口发送请求

func (*Client) HTTPDo

func (client *Client) HTTPDo(req *http.Request) (resp []byte, err error)

HTTPDo 执行 请求

func (*Client) HTTPGet

func (client *Client) HTTPGet(uri string) (resp []byte, err error)

HTTPGet GET 请求

func (*Client) HTTPPost

func (client *Client) HTTPPost(uri string, payload io.Reader, contentType string) (resp []byte, err error)

HTTPPost POST 请求

type Config

type Config struct {
	AppId     string
	AppSecret string
}

小程序配置

type GetAccessTokenFunc

type GetAccessTokenFunc func(ctx *MicroApp) (accessToken string, err error)

GetAccessTokenFunc 获取 access_token 方法接口

type MicroApp

type MicroApp struct {
	Config                         Config
	Client                         Client
	Logger                         *log.Logger
	Cache                          cachego.Cache
	GetAccessTokenHandler          GetAccessTokenFunc
	NoticeAccessTokenExpireHandler NoticeAccessTokenExpireFunc
}

MicroApp 实例

func New

func New(config Config) (microapp *MicroApp)

创建小程序实例

type NoticeAccessTokenExpireFunc

type NoticeAccessTokenExpireFunc func(ctx *MicroApp) (err error)

NoticeAccessTokenExpireFunc 通知中控 刷新 access_token

Directories

Path Synopsis
apis
auth
Package auth 登录
Package auth 登录
content_security
Package content_security 内容安全
Package content_security 内容安全
data_caching
Package data_caching 数据缓存
Package data_caching 数据缓存
qrcode
Package qrcode 二维码
Package qrcode 二维码
subscribe_notification
Package subscribe_notification 订阅消息
Package subscribe_notification 订阅消息
template_message
Package template_message 模板消息
Package template_message 模板消息
Package test 模拟服务器 测试
Package test 模拟服务器 测试

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL