me.chanjar.weixin.common.bean.WxAccessToken Java Examples

The following examples show how to use me.chanjar.weixin.common.bean.WxAccessToken. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WxMpServiceOkHttpImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
  this.log.debug("WxMpServiceOkHttpImpl is running");
  Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
  try {
    lock.lock();

    if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) {
      String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
        this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());

      Request request = new Request.Builder().url(url).get().build();
      Response response = getRequestHttpClient().newCall(request).execute();
      String resultContent = response.body().string();
      WxError error = WxError.fromJson(resultContent, WxType.MP);
      if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
      }
      WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
      this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
        accessToken.getExpiresIn());
    }
  } catch (IOException e) {
    this.log.error(e.getMessage(), e);
  } finally {
    lock.unlock();
  }
  return this.getWxMpConfigStorage().getAccessToken();
}
 
Example #2
Source File: WxAccessTokenAdapter.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
public WxAccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  WxAccessToken accessToken = new WxAccessToken();
  JsonObject accessTokenJsonObject = json.getAsJsonObject();

  if (accessTokenJsonObject.get("access_token") != null && !accessTokenJsonObject.get("access_token").isJsonNull()) {
    accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
  }
  if (accessTokenJsonObject.get("expires_in") != null && !accessTokenJsonObject.get("expires_in").isJsonNull()) {
    accessToken.setExpiresIn(GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expires_in")));
  }
  return accessToken;
}
 
Example #3
Source File: WxAccessTokenAdapter.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public WxAccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  WxAccessToken accessToken = new WxAccessToken();
  JsonObject accessTokenJsonObject = json.getAsJsonObject();

  if (accessTokenJsonObject.get("access_token") != null && !accessTokenJsonObject.get("access_token").isJsonNull()) {
    accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
  }
  if (accessTokenJsonObject.get("expires_in") != null && !accessTokenJsonObject.get("expires_in").isJsonNull()) {
    accessToken.setExpiresIn(GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expires_in")));
  }
  return accessToken;
}
 
Example #4
Source File: WxMaServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
  Lock lock = this.getWxMaConfig().getAccessTokenLock();
  try {
    lock.lock();

    if (this.getWxMaConfig().isAccessTokenExpired() || forceRefresh) {
      String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(),
        this.getWxMaConfig().getSecret());
      try {
        HttpGet httpGet = new HttpGet(url);
        if (this.getRequestHttpProxy() != null) {
          RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
          httpGet.setConfig(config);
        }
        try (CloseableHttpResponse response = getRequestHttpClient().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);
          this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(),
            accessToken.getExpiresIn());
        } finally {
          httpGet.releaseConnection();
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  } finally {
    lock.unlock();
  }

  return this.getWxMaConfig().getAccessToken();
}
 
Example #5
Source File: WxMpServiceHttpClientImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
  Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
  try {
    lock.lock();
    if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) {
      String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
        this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
      try {
        HttpGet httpGet = new HttpGet(url);
        if (this.getRequestHttpProxy() != null) {
          RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
          httpGet.setConfig(config);
        }
        try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
          String resultContent = new BasicResponseHandler().handleResponse(response);
          WxError error = WxError.fromJson(resultContent, WxType.MP);
          if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
          }
          WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
          this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
            accessToken.getExpiresIn());
        } finally {
          httpGet.releaseConnection();
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  } finally {
    lock.unlock();
  }
  return this.getWxMpConfigStorage().getAccessToken();
}
 
Example #6
Source File: WxMpServiceClusterImpl.java    From mall4j with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
        return this.getWxMpConfigStorage().getAccessToken();
    }

    RLock rLock = redissonClient.getLock(REDISSON_LOCK_PREFIX + ":WxMpServiceCluster:getAccessToken");

    try {
        boolean doingUpdateAccessToken;
        try {
            doingUpdateAccessToken = rLock.tryLock(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            return this.getWxMpConfigStorage().getAccessToken();
        }

        if (!doingUpdateAccessToken) {
            throw new YamiShopBindException("服务器繁忙,请稍后再试");
        }

        if (!this.getWxMpConfigStorage().isAccessTokenExpired()) {
            return this.getWxMpConfigStorage().getAccessToken();
        }
        String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
        String resultContent = HttpUtil.get(url);

        WxError error = WxError.fromJson(resultContent, WxType.MP);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
        this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
        return this.getWxMpConfigStorage().getAccessToken();

    } finally {
        rLock.unlock();
    }
}
 
Example #7
Source File: WxMpServiceJoddHttpImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
  Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
  try {
    lock.lock();

    if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) {
      String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
        this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());

      HttpRequest request = HttpRequest.get(url);

      if (this.getRequestHttpProxy() != null) {
        SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
        provider.useProxy(getRequestHttpProxy());

        request.withConnectionProvider(provider);
      }
      HttpResponse response = request.send();
      String resultContent = response.bodyText();
      WxError error = WxError.fromJson(resultContent, WxType.MP);
      if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
      }
      WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
      this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
        accessToken.getExpiresIn());
    }
  } finally {
    lock.unlock();
  }
  return this.getWxMpConfigStorage().getAccessToken();
}
 
Example #8
Source File: WxCpServiceApacheHttpClientImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
  if (this.configStorage.isAccessTokenExpired() || forceRefresh) {
    synchronized (this.globalAccessTokenRefreshLock) {
      if (this.configStorage.isAccessTokenExpired()) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
          + "&corpid=" + this.configStorage.getCorpId()
          + "&corpsecret=" + this.configStorage.getCorpSecret();
        try {
          HttpGet httpGet = new HttpGet(url);
          if (this.httpProxy != null) {
            RequestConfig config = RequestConfig.custom()
              .setProxy(this.httpProxy).build();
            httpGet.setConfig(config);
          }
          String resultContent = null;
          try (CloseableHttpClient httpclient = getRequestHttpClient();
               CloseableHttpResponse response = httpclient.execute(httpGet)) {
            resultContent = new BasicResponseHandler().handleResponse(response);
          } finally {
            httpGet.releaseConnection();
          }
          WxError error = WxError.fromJson(resultContent, WxType.CP);
          if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
          }
          WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
          this.configStorage.updateAccessToken(
            accessToken.getAccessToken(), accessToken.getExpiresIn());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
  }
  return this.configStorage.getAccessToken();
}
 
Example #9
Source File: WxCpServiceJoddHttpImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
  if (this.configStorage.isAccessTokenExpired() || forceRefresh) {
    synchronized (this.globalAccessTokenRefreshLock) {
      if (this.configStorage.isAccessTokenExpired()) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
          + "&corpid=" + this.configStorage.getCorpId()
          + "&corpsecret=" + this.configStorage.getCorpSecret();

        HttpRequest request = HttpRequest.get(url);
        if (this.httpProxy != null) {
          httpClient.useProxy(this.httpProxy);
        }
        request.withConnectionProvider(httpClient);
        HttpResponse response = request.send();

        String resultContent = response.bodyText();
        WxError error = WxError.fromJson(resultContent, WxType.CP);
        if (error.getErrorCode() != 0) {
          throw new WxErrorException(error);
        }
        WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
        this.configStorage.updateAccessToken(
          accessToken.getAccessToken(), accessToken.getExpiresIn());
      }
    }
  }
  return this.configStorage.getAccessToken();
}
 
Example #10
Source File: WxMaInMemoryConfig.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #11
Source File: WxMaInRedisConfig.java    From mall4j with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
@RedisLock(lockName = "updateMaAccessToken")
public void updateAccessToken(WxAccessToken accessToken) {
    updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #12
Source File: WxMpInMemoryConfigStorage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #13
Source File: WxCpInMemoryConfigStorage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #14
Source File: WxMaServiceClusterImpl.java    From mall4j with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
    if (!this.getWxMaConfig().isAccessTokenExpired() && !forceRefresh) {
        return this.getWxMaConfig().getAccessToken();
    }

    RLock rLock = redissonClient.getLock(REDISSON_LOCK_PREFIX + ":WxMaServiceCluster:getAccessToken");

    try {
        boolean lockSuccess;
        try {
            lockSuccess = rLock.tryLock(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            return this.getWxMaConfig().getAccessToken();
        }

        if (!lockSuccess) {
            throw new YamiShopBindException("服务器繁忙,请稍后再试");
        }

        if (!this.getWxMaConfig().isAccessTokenExpired()) {
            return this.getWxMaConfig().getAccessToken();
        }

        String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(),
                this.getWxMaConfig().getSecret());
        String resultContent = HttpUtil.get(url);
        WxError error = WxError.fromJson(resultContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
        this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());

        return this.getWxMaConfig().getAccessToken();

    } finally {
        rLock.unlock();
    }

}
 
Example #15
Source File: WxOpenInMemoryConfigStorage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #16
Source File: WxCpJedisConfigStorage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  this.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #17
Source File: WxMpInRedisConfigStorage.java    From mall4j with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
@RedisLock(lockName = "updateMpAccessToken")
public void updateAccessToken(WxAccessToken accessToken) {
	updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #18
Source File: WxMpInMemoryConfigStorage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #19
Source File: WxCpInMemoryConfigStorage.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void updateAccessToken(WxAccessToken accessToken) {
  updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
 
Example #20
Source File: WxMaConfig.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * 应该是线程安全的
 *
 * @param accessToken 要更新的WxAccessToken对象
 */
void updateAccessToken(WxAccessToken accessToken);
 
Example #21
Source File: WxMpConfigStorage.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * 应该是线程安全的
 * @param accessToken
 */
public void updateAccessToken(WxAccessToken accessToken);
 
Example #22
Source File: WxMpConfigStorage.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * 应该是线程安全的
 *
 * @param accessToken 要更新的WxAccessToken对象
 */
void updateAccessToken(WxAccessToken accessToken);
 
Example #23
Source File: WxCpConfigStorage.java    From weixin-java-tools with Apache License 2.0 votes vote down vote up
void updateAccessToken(WxAccessToken accessToken); 
Example #24
Source File: WxCpConfigStorage.java    From weixin-java-tools with Apache License 2.0 votes vote down vote up
public void updateAccessToken(WxAccessToken accessToken);