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

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