GithubHelp home page GithubHelp logo

xhs-sdk's Introduction

小红书开放平台 GO SDK

Go语言实现小红书开放平台sdk,使用简单,扩展灵活

平台链接

安装命令

go get github.com/zsmhub/xhs-sdk

sdk调用示例

强烈建议去 ./demo 文件夹查看完整示例!

点击查看完整demo

推送消息调用示例

e := echo.New()

e.GET("/message/auth", func(c echo.Context) error {
    // 打印消息推送请求体,便于调试
    // requestBody, _ := ioutil.ReadAll(c.Request().Body)
    // fmt.Printf("xhs auth : uri=%s, req=%s\n", c.Request().RequestURI, string(requestBody))

    code := c.Param("code")
    state := c.Param("state")
    fmt.Printf("code=%s, state=%s\n", code, state)

    return c.JSON(http.StatusOK, message.MessageResp{
        Success:   true,
        ErrorCode: 0,
        ErrorMsg:  "",
    })
}).Name = "应用授权回调地址"

e.POST("/message/push", func(c echo.Context) error {
    // 打印消息推送请求体,便于调试
    // requestBody, _ := ioutil.ReadAll(c.Request().Body)
    // fmt.Printf("xhs push : uri=%s, req=%s\n", c.Request().RequestURI, string(requestBody))

    var req []message.Message
    _ = c.Bind(&req)
    for _, v := range req {
        vJson, _ := json.Marshal(v)
        fmt.Printf("msg=%s\n", vJson)

        // 消息data解析示例
        if v.MsgTag == (message.MsgFulfillmentStatusChange{}.MsgTag()) {
            // 方案一
            //var resp message.MsgFulfillmentStatusChange
            //if err := v.DecodeData(&resp); err != nil {
            //    fmt.Println(err)
            //    continue
            //} else {
            //    fmt.Printf("resp=%+v\n", resp)
            //}

            // 方案二
            resp, err := message.MsgFulfillmentStatusChange{}.DecodeData(v.Data)
            if err != nil {
                fmt.Println(err)
                continue
            } else {
                fmt.Printf("resp=%+v\n", resp)
            }
        }
    }

    return c.JSON(http.StatusOK, message.MessageResp{
        Success:   true,
        ErrorCode: 0,
        ErrorMsg:  "",
    })
}).Name = "应用推送回调地址"

api sdk 调用示例

func GetOAuthUrl(redirectUri, state string) {
	authUrl := xhs_sdk.NewXhsOAuthClient(AppKey, AppSecret).GetOAuthUrl(redirectUri, state)
	fmt.Printf("authUrl=%s\n", authUrl)
}

func GetAccessToken() {
	accessToken, err := xhs_sdk.NewXhsOAuthClient(AppKey, AppSecret).GetAccessToken(Code)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%+v\n", accessToken)
}

func GetExpressCompanyList() {
	var resp api.RespGetExpressCompanyList
	err := xhs_sdk.NewXhsClient(AppKey, AppSecret).SetAccessToken(AccessToken).Post(api.ReqGetExpressCompanyList{}, &resp)
	if err != nil {
		fmt.Println(err)
		return
	}
	ret, _ := json.Marshal(resp)
	fmt.Printf("%s\n", string(ret))
}

目录结构

.
├── api          调用API
├── message      推送消息
├── demo         sdk调用示例
├── constant.go  全局枚举值定义
└── sdk.go       入口文件

注意点

  1. 如果你发现了sdk中,没有某个推送消息或某个api,可自行加上,然后提交下pr
  2. 消息推送机制不健全:订单待支付状态时没有创建订单的消息推送,售后状态变更没有消息推送,需要自行用定时任务轮询小红书接口获取
  3. 由于授权码有效期为10分钟,为了避免店铺重新授权的情况,我们需要用定时任务定时刷新 access_token,保证不过期

推荐开源项目

xhs-sdk's People

Contributors

zsmhub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.