github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/oauth2/endpoint.go (about)

     1  package oauth2
     2  
     3  import (
     4  	"net/url"
     5  
     6  	"github.com/chanxuehong/wechat/oauth2"
     7  )
     8  
     9  var _ oauth2.Endpoint = (*Endpoint)(nil)
    10  
    11  // Endpoint 实现了 github.com/chanxuehong/wechat/oauth2.Endpoint 接口.
    12  type Endpoint struct {
    13  	AppId     string
    14  	AppSecret string
    15  }
    16  
    17  func NewEndpoint(AppId, AppSecret string) *Endpoint {
    18  	return &Endpoint{
    19  		AppId:     AppId,
    20  		AppSecret: AppSecret,
    21  	}
    22  }
    23  
    24  func (p *Endpoint) ExchangeTokenURL(code string) string {
    25  	return "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + url.QueryEscape(p.AppId) +
    26  		"&secret=" + url.QueryEscape(p.AppSecret) +
    27  		"&code=" + url.QueryEscape(code) +
    28  		"&grant_type=authorization_code"
    29  }
    30  
    31  func (p *Endpoint) RefreshTokenURL(refreshToken string) string {
    32  	return "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + url.QueryEscape(p.AppId) +
    33  		"&grant_type=refresh_token&refresh_token=" + url.QueryEscape(refreshToken)
    34  }
    35  
    36  func (p *Endpoint) SessionCodeUrl(code string) string {
    37  
    38  	return "https://api.weixin.qq.com/sns/jscode2session?appid=" + url.QueryEscape(p.AppId) +
    39  		"&secret=" + url.QueryEscape(p.AppSecret) +
    40  		"&js_code=" + url.QueryEscape(code) +
    41  		"&grant_type=authorization_code"
    42  }