溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Golang怎么通過小程序獲取微信openid

發布時間:2021-04-12 17:57:50 來源:億速云 閱讀:258 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關使用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了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女