Payments & Platform Access · Practical News

Mini Program code-to-openid Exchange Fails: Reading the Errors

Source: Paddy Dance teamKeywords: mini program code2session failed

What the errors mean

Code already used or expired means the same code was submitted twice, typically from double taps or an uncontrolled server retry.

Invalid appid or secret usually comes from a copied value with trailing whitespace, or from mixing in an official account appid. Official account and mini program appids are not interchangeable.

The correct single-use flow

The client calls wx.login, sends the code to your backend, and the backend calls jscode2session with code, appid and secret to obtain openid and session_key.

session_key stays on the server for decrypting sensitive data. Never send it to the client or log it in full.

Maintaining the session

Do not fetch openid on every request. After the first login, issue your own token and identify users with it.

session_key expires. When decryption fails, have the client call wx.login again for a new code. That is normal flow, not an exception.

FAQ

How long is a code valid?

Minutes at most, and only once. Use it immediately and never cache it.

Can the client call jscode2session directly?

No. It requires the secret and must only be called server-side. Client-side calls expose the secret.

Does openid change for the same user?

Within one mini program, openid is stable. A different mini program or entity produces a different openid; use unionid to link them.

Related questions

code already used or expiredjscode2session errorsmini program login troubleshootingappid and secret mismatch

Further reading

Sources & References

The following are public sources. Always check the latest requirements on the official authority or platform page.

Explain your case first, then confirm the scope

Tell us your entity, business model and current progress. We will confirm what we can assist with and what you need to do before discussing fees.