当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
755 Star 2K Fork 745

sd4324530 / fastweixin
暂停

 / 详情

自动刷新token不生效

已完成
创建于  
2014-12-02 10:02

自动刷新access_token貌似不生效哦,看了一下BaseAPI里面的executePost方法,
if(null == response || ResultType.ACCESS_TOKEN_TIMEOUT.toString().equals(response.getErrcode())) {
if(!config.refreshing.get()) {
refreshToken();
}
readLock.lock();
try {
TimeUnit.SECONDS.sleep(1);
response = NetWorkCenter.post(url, json); // 此时URL中的#已被第一次请求时replace掉了,再次重试时access_token并没有换成刷新后的token
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readLock.unlock();
}
}

评论 (10)

DEBUG下发现连判断token是否失效都没进哦。。

if(null == response || ResultType.ACCESS_TOKEN_TIMEOUT.getCode().toString().equals(response.getErrcode()))

这个是修改后的判断,之前的判断是有问题的,希望即时更新啊

package com.github.sd4324530.fastweixin.api;

import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.enums.ResultType;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.api.response.GetTokenResponse;
import com.github.sd4324530.fastweixin.util.BeanUtil;
import com.github.sd4324530.fastweixin.util.JSONUtil;
import com.github.sd4324530.fastweixin.util.NetWorkCenter;
import org.apache.http.HttpStatus;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * API基类,提供一些通用方法
 * 包含自动刷新token、通用get post请求等
 * @author peiyu
 * @since 1.2
 */
public abstract class BaseAPI {
    protected static final String BASE_API_URL = "https://api.weixin.qq.com/";
    protected static final String FILE_API_URL = "http://file.api.weixin.qq.com/";

    protected final ApiConfig config;

    //用于刷新token时锁住config,防止多次刷新
    private final ReadWriteLock lock = new ReentrantReadWriteLock();

    private final Lock readLock = lock.readLock();

    private final Lock writeLock = lock.writeLock();

    public BaseAPI(ApiConfig config) {
        this.config = config;
    }

    /**
     * 刷新token
     */
    protected void refreshToken() {
        writeLock.lock();
        try {
            if(config.refreshing.compareAndSet(false, true)) {
                String url = BASE_API_URL + "cgi-bin/token?grant_type=client_credential&appid=" + this.config.getAppid() + "&secret=" + this.config.getSecret();
                NetWorkCenter.get(url, null, new NetWorkCenter.ResponseCallback() {
                    @Override
                    public void onResponse(int resultCode, String resultJson) {
                        if (HttpStatus.SC_OK == resultCode) {
                            GetTokenResponse response = JSONUtil.toBean(resultJson, GetTokenResponse.class);
                            BaseAPI.this.config.setAccess_token(response.getAccess_token());
                        }
                    }
                });
            }
        } finally {
            config.refreshing.set(false);
            writeLock.unlock();
        }
    }

    /**
     * 通用post请求
     * @param url 地址,其中token用#代替
     * @param json 参数,json格式
     * @return 请求结果
     */
    protected BaseResponse executePost(String url, String json) {
        BaseResponse response = null;
        BeanUtil.requireNonNull(url, "url is null");
        String postURL = null;
        readLock.lock();
        try {
        	//需要传token
        	postURL = url.replace("#",config.getAccess_token());
            response = NetWorkCenter.post(postURL, json);
        } finally {
            readLock.unlock();
        }

        if(null == response || ResultType.ACCESS_TOKEN_TIMEOUT.getCode().toString().equals(response.getErrcode())) {
            if(!config.refreshing.get()) {
                refreshToken();
            }
            readLock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
                postURL = url.replace("#",config.getAccess_token());
                response = NetWorkCenter.post(postURL, json);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readLock.unlock();
            }
        }

        return response;
    }

    /**
     * 通用post请求
     * @param url 地址,其中token用#代替
     * @param json 参数,json格式
     * @param fileList 参数
     * @return 请求结果
     */
    protected BaseResponse executePost(String url, String json, List<File> fileList) {
        BaseResponse response = null;
        BeanUtil.requireNonNull(url, "url is null");
        String postURL = null;
        readLock.lock();
        try {
        	//需要传token
        	postURL = url.replace("#",config.getAccess_token());
            response = NetWorkCenter.post(postURL, json, fileList);
        } finally {
            readLock.unlock();
        }

        if(null == response || ResultType.ACCESS_TOKEN_TIMEOUT.getCode().toString().equals(response.getErrcode())) {
            if(!config.refreshing.get()) {
                refreshToken();
            }
            readLock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
                postURL = url.replace("#",config.getAccess_token());
                response = NetWorkCenter.post(postURL, json, fileList);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readLock.unlock();
            }
        }

        return response;
    }
    
    /**
     * 通用post请求
     * @param url 地址,其中token用#代替
     * @return 请求结果
     */
    protected BaseResponse executeGet(String url) {
        BaseResponse response = null;
        BeanUtil.requireNonNull(url, "url is null");
        String getURL = null;
        readLock.lock();
        try {
            //需要传token
        	getURL = url.replace("#",config.getAccess_token());
            response = NetWorkCenter.get(getURL);
        } finally {
            readLock.unlock();
        }

        if(null == response || ResultType.ACCESS_TOKEN_TIMEOUT.getCode().toString().equals(response.getErrcode())) {
            if (!config.refreshing.get()) {
                refreshToken();
            }
            readLock.lock();
            try {
                TimeUnit.SECONDS.sleep(1);
                getURL = url.replace("#",config.getAccess_token());
                response = NetWorkCenter.get(getURL);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readLock.unlock();
            }
        }
        return response;
    }
}

这个是修改后的BaseAPI类

感恩反馈,已经修复

状态更改为 已关闭

提交者更改状态为 已关闭 by commit 0d4b834a

提交者更改状态为 已关闭 by commit 0d4b834a

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
254 imvenzee 1578913757 113628 pyinjava 1578918112
Java
1
https://gitee.com/pyinjava/fastweixin.git
git@gitee.com:pyinjava/fastweixin.git
pyinjava
fastweixin
fastweixin

搜索帮助