12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
-
-
- package webpage
-
-
-
- import (
- "encoding/json"
- "net/url"
- "strings"
-
- "gitee.com/yansen_zh/wxcomponent/config"
- "gitee.com/yansen_zh/wxcomponent/errors"
- "gitee.com/yansen_zh/wxcomponent/utils/request"
- )
-
-
-
- func GetOAuthCodeLink(appID, redirectURI, state string, scopes ...string) string {
- params := url.Values{}
- params.Set("appid", appID)
- params.Set("redirect_uri", redirectURI)
- params.Set("response_type", "code")
- params.Set("scope", strings.Join(scopes, ","))
- params.Set("state", state)
- params.Set("component_appid", config.GetAppID())
-
- return "https://open.weixin.qq.com/connect/oauth2/authorize?" + params.Encode()
- }
-
-
- type MpOAuthAccessTokenResult struct {
- errors.Error
-
- AccessToken string `json:"access_token"`
-
- ExpiresIn int `json:"expires_in"`
-
- RefreshToken string `json:"refresh_token"`
-
- OpenID string `json:"openid"`
-
- Scope string `json:"scope"`
- }
-
- const apiOAuthAccessToken = "https://api.weixin.qq.com/sns/oauth2/component/access_token"
-
-
- func GetOAuthAccessToken(appID, code string) (*MpOAuthAccessTokenResult, error) {
- if appID == "" || code == "" {
- return nil, errors.New("获取网页授权Token appID 或者 code 不能为空")
- }
-
- param := url.Values{}
- param.Set("appid", appID)
- param.Set("code", code)
- param.Set("grant_type", "authorization_code")
- param.Set("component_appid", config.GetAppID())
- param.Set("component_access_token", config.GetAccessToken())
-
- resp, e2 := request.GetJSON(apiOAuthAccessToken, ¶m)
- if e2 != nil {
- return nil, e2
- }
-
- result := MpOAuthAccessTokenResult{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- return &result, nil
- }
|