使用Golang怎么通过小程序获取微信openid

发布时间:2021-04-12 17:57:50 作者:Leah
来源:亿速云 阅读:233

这期内容当中小编将会给大家带来有关使用Golang怎么通过小程序获取微信openid,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

小程序获取 openid 的流程

那么小程序获取 openid 的流程具体如下

使用Golang怎么通过小程序获取微信openid

我们需要在小程序中调用 wx.login() 获取 code 码,然后将这个 code 码发送给后端,后端带着这个 code 码和 appid,appsecret 向微信接口发起 http 请求获取 openid。

注意事项

在开发的小程序中的 AppID 一定要和后端使用的 AppID 保持一致,否则会获取 openid 失败

使用Golang怎么通过小程序获取微信openid

我们请求的微信 API 为 auth.code2Session ,

请求地址为:

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

所需的四个参数为:

属性类型默认值必填说明
appidstring
小程序 appId
secretstring
小程序 appSecret
js_codestring
登录时获取的 code
grant_typestring
授权类型,此处只需填写 authorization_code

js_code 就是我们通过 wx.login 得到的 code,grant_type 为 authorization_code,只剩下 appid 和 secret 需要我们登录微信公总平台 里面找

使用Golang怎么通过小程序获取微信openid 

小程序代码演示

为了方便操作,我们在 index 页面编写了一个 button,通过 button 触发事件

<!--index.wxml-->
<view class="container">
 <button bindtap="onGetOpenId">点击获取openid</button>
</view>

然后编写事件函数:

//index.js
Page({
 onGetOpenId() {
  wx.login({
   success: res => {
    if (res.code) {
     wx.request({
      url: "http://localhost:2020/openid",
      method: "POST",
      data: {
       code: res.code
      },
      success: res => {
       console.log(res);
      }
     });
    }
   }
  });
 }
});

那么,在小程序中发送 http 请求强制要求地址必须为 https,由于我们在开发中,我们可以把强制 https 的设置关闭

使用Golang怎么通过小程序获取微信openid 

Go 语言后端代码演示

小程序发过来的数据和去微信 API 获取的数据都是放在 http body 里,所以我们要从 body 获取

package main

import (
  "encoding/json"
  "fmt"
  "net/http"
)

func main() {
  http.HandleFunc("/openid", getOpenID)
  http.ListenAndServe(":2020", nil)
}

func getOpenID(writer http.ResponseWriter, request *http.Request) {
  if request.Method != http.MethodPost {
    return
  }

  var codeMap map[string]string
  err := json.NewDecoder(request.Body).Decode(&codeMap)
  if err != nil {
    return
  }
  defer request.Body.Close()

  code := codeMap["code"]
  openid, err := sendWxAuthAPI(code)
  if err != nil {
    return
  }
  fmt.Println("my openid", openid)
}

const (
  code2sessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
  appID      = "你的AppID"
  appSecret    = "你的AppSecret"
)

func sendWxAuthAPI(code string) (string, error) {
  url := fmt.Sprintf(code2sessionURL, appID, appSecret, code)
  resp, err := http.DefaultClient.Get(url)
  if err != nil {
    return "", err
  }
  var wxMap map[string]string
  err = json.NewDecoder(resp.Body).Decode(&wxMap)
  if err != nil {
    return "", err
  }
  defer resp.Body.Close()

  return wxMap["openid"], nil
}

运行结果

运行代码,在小程序中点击:

使用Golang怎么通过小程序获取微信openid

结果:

使用Golang怎么通过小程序获取微信openid

上述就是小编为大家分享的使用Golang怎么通过小程序获取微信openid了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. php如何获取微信openid
  2. 微信小程序获取用户openid的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang openid 小程序

上一篇:如何使用Go语言下载网络图片

下一篇:使用JavaScript怎么实现一个放大镜效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》