Search in sources :

Example 16 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method executeInternal.

protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
    if (uri.indexOf("access_token=") != -1) {
        throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
    }
    String accessToken = getAccessToken(false);
    String uriWithAccessToken = uri;
    uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
    try {
        return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
    } catch (WxErrorException e) {
        WxError error = e.getError();
        /*
       * 发生以下情况时尝试刷新access_token
       * 40001 获取access_token时AppSecret错误,或者access_token无效
       * 42001 access_token超时
       */
        if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
            // 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
            wxCpConfigStorage.expireAccessToken();
            return execute(executor, uri, data);
        }
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return null;
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 17 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method materialNewsUpdate.

public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/material/update_news";
    String responseText = post(url, wxMpMaterialArticleUpdate.toJson());
    WxError wxError = WxError.fromJson(responseText);
    if (wxError.getErrorCode() == 0) {
        return true;
    } else {
        throw new WxErrorException(wxError);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 18 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method materialFileBatchGet.

public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
    String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
    Map<String, Object> params = new HashMap<>();
    params.put("type", type);
    params.put("offset", offset);
    params.put("count", count);
    String responseText = post(url, WxGsonBuilder.create().toJson(params));
    WxError wxError = WxError.fromJson(responseText);
    if (wxError.getErrorCode() == 0) {
        return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
    } else {
        throw new WxErrorException(wxError);
    }
}
Also used : WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 19 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method getAccessToken.

public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (forceRefresh) {
        wxMpConfigStorage.expireAccessToken();
    }
    if (wxMpConfigStorage.isAccessTokenExpired()) {
        synchronized (globalAccessTokenRefreshLock) {
            if (wxMpConfigStorage.isAccessTokenExpired()) {
                String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + wxMpConfigStorage.getAppId() + "&secret=" + wxMpConfigStorage.getSecret();
                try {
                    HttpGet httpGet = new HttpGet(url);
                    if (httpProxy != null) {
                        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
                        httpGet.setConfig(config);
                    }
                    CloseableHttpResponse response = getHttpclient().execute(httpGet);
                    String resultContent = new BasicResponseHandler().handleResponse(response);
                    WxError error = WxError.fromJson(resultContent);
                    if (error.getErrorCode() != 0) {
                        throw new WxErrorException(error);
                    }
                    WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
                    wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
                } catch (ClientProtocolException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    return wxMpConfigStorage.getAccessToken();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) WxAccessToken(me.chanjar.weixin.common.bean.WxAccessToken) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 20 with WxError

use of me.chanjar.weixin.common.bean.result.WxError in project weixin-java-tools by chanjarster.

the class MaterialUploadRequestExecutor method execute.

public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig response = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(response);
    }
    if (material != null) {
        File file = material.getFile();
        if (file == null || !file.exists()) {
            throw new FileNotFoundException();
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.addPart("media", new InputStreamBody(bufferedInputStream, material.getName())).setMode(HttpMultipartMode.RFC6532);
        Map<String, String> form = material.getForm();
        if (material.getForm() != null) {
            multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
        }
        httpPost.setEntity(multipartEntityBuilder.build());
        httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
    }
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return WxMpMaterialUploadResult.fromJson(responseContent);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody)

Aggregations

WxError (me.chanjar.weixin.common.bean.result.WxError)23 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)19 RequestConfig (org.apache.http.client.config.RequestConfig)11 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 HttpPost (org.apache.http.client.methods.HttpPost)8 HashMap (java.util.HashMap)6 StringEntity (org.apache.http.entity.StringEntity)6 IOException (java.io.IOException)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 JsonObject (com.google.gson.JsonObject)2 WxAccessToken (me.chanjar.weixin.common.bean.WxAccessToken)2 RequestExecutor (me.chanjar.weixin.common.util.http.RequestExecutor)2 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)2 DataProvider (org.testng.annotations.DataProvider)2 XStream (com.thoughtworks.xstream.XStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 TreeMap (java.util.TreeMap)1 WxRedpackResult (me.chanjar.weixin.mp.bean.result.WxRedpackResult)1