您的位置:首页 > 移动开发 > 微信开发

微信开放平台找回过期的authorizer_access_token

2018-03-07 10:53 851 查看
  当微信公众号授权给第三方开发平台后,第三方开发平台执行某些操作时,如查询用户信息、用户打标签,需要使用authorizer_access_token。这个token从获得开始,2小时内有效,如果需要继续使用授权,就需要在有效期内主动刷新token。但是当某些原因导致刷新token失败时,仍然可以使用authorizer_refresh_token(自获得起30天内有效)重新获得authorizer_access_token。
本文中使用的代码均来自github的项目weixin-popular-2.8.6.jarweixin.popular.api.ComponentAPI

找回authorizer_access_token需要先获得两个数据【component_access_token】和【authorizer_refresh_token】

1. 获取component_access_token

1.1 微信服务器会向其“授权事件接收URL”每隔10分钟定时推送component_verify_ticket
授权事件接收URL在【微信开放平台->第三方平台->开发资料->授权事件接收URL】中修改

1.2 使用【微信开放平台->第三方平台->AppID】【微信开放平台->第三方平台->AppSecret,即component_appsecret】和第一步的component_verify_ticket获取公众号第三方平台access_token,即component_access_token
/**
* 获取公众号第三方平台access_token
* @param component_appid 公众号第三方平台appid
* @param component_appsecret 公众号第三方平台appsecret
* @param component_verify_ticket 微信后台推送的ticket,此ticket会定时推送,具体请见本页末尾的推送说明
* @return 公众号第三方平台access_token
*/
public static ComponentAccessToken api_component_token(String component_appid,String component_appsecret,String component_verify_ticket){
String postJsonData = String.format("{\"component_appid\":\"%1$s\" ,\"component_appsecret\": \"%2$s\",\"component_verify_ticket\": \"%3$s\"}",
component_appid,
component_appsecret,
component_verify_ticket);
HttpUriRequest httpUriRequest = RequestBuilder.post()
.setHeader(jsonHeader)
.setUri(BASE_URI + "/cgi-bin/component/api_component_token")
.setEntity(new StringEntity(postJsonData,Charset.forName("utf-8")))
.build();
return LocalHttpClient.executeJsonResult(httpUriRequest,ComponentAccessToken.class);
}

2. 公众号授权,获得authorizer_refresh_token

转向以下页面,用户扫码授权。授权完成后,会重定向到redirect_uri https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={component_appid}&pre_auth_code={pre_auth_code}&redirect_uri={redirect_uri} 在redirect_uri方法中会接收到一个auth_code参数,然后使用该参数获取授权信息/**
* 使用授权码换取公众号的授权信息
* @param component_access_token component_access_token
* @param component_appid 公众号第三方平台appid
* @param authorization_code 授权code,会在授权成功时返回给第三方平台,详见第三方平台授权流程说明
* @return 公众号的授权信息
*/
public static ApiQueryAuthResult api_query_auth(String component_access_token,String component_appid,String authorization_code){
String postJsonData = String.format("{\"component_appid\":\"%1$s\",\"authorization_code\":\"%2$s\"}",
component_appid,
authorization_code);
HttpUriRequest httpUriRequest = RequestBuilder.post()
.setHeader(jsonHeader)
.setUri(BASE_URI + "/cgi-bin/component/api_query_auth")
.addParameter("component_access_token", API.componentAccessToken(component_access_token))
.setEntity(new StringEntity(postJsonData,Charset.forName("utf-8")))
.build();
return LocalHttpClient.executeJsonResult(httpUriRequest,ApiQueryAuthResult.class);
}
从授权信息中即可获得authorizer_refresh_token

3. 使用第1步获取的component_access_token和第2步获取的authorizer_refresh_token获取authorizer_access_token

/**
* 获取(刷新)授权公众号的令牌
* @param component_access_token component_access_token
* @param component_appid	公众号第三方平台appid
* @param authorizer_appid	授权方appid
* @param authorizer_refresh_token	授权方的刷新令牌,刷新令牌主要用于公众号第三方平台获取和刷新已授权用户的access_token,只会在授权时刻提供,请妥善保存。 一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌
* @return 授权公众号的令牌
*/
public static AuthorizerAccessToken api_authorizer_token(String component_access_token,
String component_appid,
String authorizer_appid,
String authorizer_refresh_token){
String postJsonData = String.format("{\"component_appid\":\"%1$s\",\"authorizer_appid\":\"%2$s\",\"authorizer_refresh_token\":\"%3$s\"}",
component_appid,authorizer_appid,authorizer_refresh_token);
HttpUriRequest httpUriRequest = RequestBuilder.post()
.setHeader(jsonHeader)
.setUri(BASE_URI + "/cgi-bin/component/api_authorizer_token")
.addParameter("component_access_token", API.componentAccessToken(component_access_token))
.setEntity(new StringEntity(postJsonData,Charset.forName("utf-8")))
.build();
return LocalHttpClient.executeJsonResult(httpUriRequest,AuthorizerAccessToken.class);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐