You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/service/wechatminiprogram/cgi-bin.token.go

39 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package wechatminiprogram
import (
"encoding/json"
"fmt"
"github.com/dtapps/go-library/utils/gohttp"
"net/http"
)
type AuthGetAccessTokenResponse struct {
AccessToken string `json:"access_token"` // 获取到的凭证
ExpiresIn int `json:"expires_in"` // 凭证有效时间单位秒。目前是7200秒之内的值
Errcode int `json:"errcode"` // 错误码
Errmsg string `json:"errmsg"` // 错误信息
}
type AuthGetAccessTokenResult struct {
Result AuthGetAccessTokenResponse // 结果
Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误
}
func NewAuthGetAccessTokenResult(result AuthGetAccessTokenResponse, body []byte, http gohttp.Response, err error) *AuthGetAccessTokenResult {
return &AuthGetAccessTokenResult{Result: result, Body: body, Http: http, Err: err}
}
// AuthGetAccessToken
// 接口调用凭证
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
func (app *App) AuthGetAccessToken() *AuthGetAccessTokenResult {
// 请求
request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", app.AppId, app.AppSecret), map[string]interface{}{}, http.MethodGet)
// 定义
var response AuthGetAccessTokenResponse
err = json.Unmarshal(request.Body, &response)
return NewAuthGetAccessTokenResult(response, request.Body, request, err)
}