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

     1  package oauth2
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  
     7  	mpoauth2 "github.com/chanxuehong/wechat/mp/oauth2"
     8  )
     9  
    10  // AuthCodeURL 生成网页授权地址.
    11  //
    12  //	appId:       公众号的唯一标识
    13  //	redirectURI: 授权后重定向的回调链接地址
    14  //	scope:       应用授权作用域
    15  //	state:       重定向后会带上 state 参数, 开发者可以填写 a-zA-Z0-9 的参数值, 最多128字节
    16  func AuthCodeURL(appId, redirectURI, scope, state string) string {
    17  	return "https://open.weixin.qq.com/connect/qrconnect?appid=" + url.QueryEscape(appId) +
    18  		"&redirect_uri=" + url.QueryEscape(redirectURI) +
    19  		"&response_type=code&scope=" + url.QueryEscape(scope) +
    20  		"&state=" + url.QueryEscape(state) +
    21  		"#wechat_redirect"
    22  }
    23  
    24  // Auth 检验授权凭证 access_token 是否有效.
    25  //
    26  //	accessToken: 网页授权接口调用凭证
    27  //	openId:      用户的唯一标识
    28  //	httpClient:  如果不指定则默认为 util.DefaultHttpClient
    29  func Auth(accessToken, openId string, httpClient *http.Client) (valid bool, err error) {
    30  	return mpoauth2.Auth(accessToken, openId, httpClient)
    31  }