This is a creation in Article, where the information may have evolved or changed.
We are using the Golang rapid development of the public Platform (iii): The custom menu has been added to enter the Mall button, and this button is created with a URL, but you do not go in. Because this page is not licensed.
Before you begin, there are a few things to prepare for:
- If you have not read the page authorization document in detail before reading this article, please stop to see it at once.
- If you are the same as me before, to write the Web page is not cold, the document to see vomit did not see why, you can then look down
The first step is to add a route to the project, the route to the page to be able to access the browser normally assuming that the current address is http://www.baidu.com/wx_index.
Add route
Render page
Manually knocking addresses in the browser
Second step modification using Golang rapid development Public Platform (iii): custom menu to enter the URL of the mall to make the two consistent
Step three Add the page authorization callback domain name
About this domain name to fill in, if your domain name is http://www.baidu.com then you fill in www.baidu.com enough straightforward
Here's a moment to witness a miracle.
Note that the scope=snsapi_base here may be different from yours.
Getwxidinwxshop.go
Package Getwxidinwxshopimport ("Strings" "Github.com/astaxie/beego" "Net/http" "FMT" "Io/ioutil" "bytes" "Encoding/json" "Github.com/astaxie/beego/context" "Net/url" "Rbearserver/models" "Github.com/astaxie/beego/orm" "Rbearserver/shoputils/logutils"//suburl is similar to/user_view/wx_user_edit such as the specific can go to router lookup//idea is to get code through OAUTH2 first and then get OpenID according to code finally redirect to this page func Getwxid ( C *context. Context, Suburl String) (OpenID string) {wxbase: = Models. Wxbase{Id:1} ERR: = ORM. Neworm(). Read(&wxbase,"id") If err! = Nil {FMT. Println("Read Wxbase failed", err) logutils. GetLog(). Error("Read Wxbase failed", err) return""} if code: = C. Input. Query("Code"); code = = "" {Encodurl: = URL. Queryescape(Beego. AppConfig. String("BaseURL") + Suburl) Urlstr: ="Https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ wxbase. AppID+"&redirect_uri="+ Encodurl +"&response_type=code&scope=snsapi_base&state=state#wechat_redirect"C. Redirect(302, URLSTR)} else {if sn, err: = GetWxOpendidFromoauth2 (Code, Wxbase. AppID, Wxbase. Appsecret)Err = = Nil {OpenID = SN. Openid}} Return}func getWxOpendidFromoauth2 (code, AppID, Secret string) (*models. Snsapibase, error) {requestline: = strings. Join([]string{"Https://api.weixin.qq.com/sns/oauth2/access_token","? appid=", AppID,"&secret=", Secret,"&code=", Code,"&grant_type=authorization_code"},"") RESP, err: = http. Get(requestline) If err! = Nil | | Resp. StatusCode! = http. Statusok{FMT. Println("Send GET request to get OpenID error", err) logutils. GetLog(). Error("Send GET request to get OpenID error", err) return nil, err} defer resp. Body. Close() body, err: = Ioutil. ReadAll(RESP. Body) If err! = Nil {FMT. Println("Send GET request to get OpenID read return body error", err) logutils. GetLog(). Error("Send GET request to get OpenID read return body error", err) return nil, err} if bytes. Contains(Body, []byte ("Errcode") {ater: = Models. Accesstokenerrorresponse{} err = JSON. Unmarshal(Body, &ater) if err! = Nil {FMT. Printf("Send GET request to get OpenID error message%+v\n", ater) logutils. GetLog(). Error("Send GET request to get OpenID error message%+v\n", ater) return nil, err} return nil, FMT. Errorf('%s ', ater. ErrMsg)} else {ATR: = Models. Snsapibase{} err = JSON. Unmarshal(Body, &ATR) if err! = Nil {FMT. Println("Send GET request to get OpenID return data JSON parse error", err) logutils. GetLog(). Error("Send GET request to get OpenID return data JSON parse error", err) return nil, err} return &atr, nil}}
And the previous rendering page has become this way.
We opened the Web Developer tool to enter this URL to take a look (require binding developers first)
Note: Redirect_uri parameters require the use of UrlEncode to handle the link, where the link is shaped like http://www.baidu.cn/xxxx must be with the WWW must be with HTTP//less will be reported Redirect_uri parameter error , which is already very good, and the later payment will come to a more collapsing
Above, call it off.