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())
if access_token_cache['access_token'] and access_token_cache['expires_time'] > current_time:
return access_token_cache['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']
access_token_cache['expires_time'] = current_time + data['expires_in'] - 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,是哪里有问题吗?能帮我改改吗?谢谢
你好,提供下返回的rid
rid: 6711f741-3aa94e06-51bc0419'
帖子里面也有,谢谢
你确定没这个是微信小程序登录并获取用户信息,
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("in denglu_get_wechat_session_for_unionId, user_info=", user_info)
我也用的这个url,获取unionid,不知道为何老过期
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html