收藏
回答

微信小程序登录,拿Access Token后拿unionid,总是提示40001错误,是怎么回事?

access_token =get_fuwuhao_access_code_token(appid, secret)
# 获取用户信息
user_info_url = f"https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={openid}&lang=zh_CN"
user_info_response = requests.get(user_info_url)
user_info = user_info_response.json()
print("user_info=", user_info)

供调用的get_fuwuhao_access_code_token(appid, secret)函数:
def get_fuwuhao_access_code_token(appid, secret):
    global access_token_cache
    current_time = int(time.time())
    # 检查access_token是否过期
    if access_token_cache['access_token'] and access_token_cache['expires_time'] > current_time:
        return access_token_cache['access_token']
    # Access Token已过期,重新请求
    url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"
    response = requests.get(url)
    data = response.json()
    if "access_token" in data:
        access_token_cache['access_token'] = data['access_token']
        # Token的有效期为7200秒
        access_token_cache['expires_time'] = current_time + data['expires_in'] - 200  # 提前200秒请求
        print("Access Token:为什么会有两次???", access_token_cache['access_token'])
        return access_token_cache['access_token']
    else:
        print(f"Error getting access token: {data}")
        return None

输出:
Access Token:为什么会有两次??? 85_hnWbFNovs2Ka8igUyTXVGSU2*****XsDKdzztBsGLZfADANSG
user_info= {'errcode': 40001, 'errmsg': 'invalid credential, access_token is invalid or not latest, rid: 6711f741-3aa94e06-51bc0419'}
老是报这个40001的错误,说access_token无效或不是最新的。我想拿登录小程序的用户的unionid,是哪里有问题吗?能帮我改改吗?谢谢
回答关注问题邀请回答
收藏

4 个回答

登录 后发表内容