package kuaishou_mini

import (
	"errors"
	"git.rosy.net.cn/baseapi/utils"
)

// AuthLoginKuaiShou 快手授权登录
func (a *API) AuthLoginKuaiShou(jsCode string) (sessionKey, openId string, err error) {
	if a.appId == "" || a.appSecret == "" || jsCode == "" {
		return "", "", err
	}

	result, err := a.AccessAPI2(KuaiShouAuthLogin, map[string]interface{}{"js_code": jsCode, "app_id": a.appId, "app_secret": a.appSecret})
	if err != nil {
		return "", "", err
	}

	auth := GetLoginAuth{}
	if err := utils.Map2StructByJson(result, &auth, false); err != nil {
		return "", "", err
	}

	if auth.Error != "" {
		return "", "", errors.New(auth.ErrorMsg)
	}

	return auth.SessionKey, auth.OpenId, nil
}
