• Spring-Security-OAuth2调用微信API


    class WeixinClientCredentialsAccessTokenProvider extends ClientCredentialsAccessTokenProvider {

    public WeixinClientCredentialsAccessTokenProvider() {

    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

    converters.add(new MappingJackson2HttpMessageConverter());

    this.setMessageConverters(converters);

    this.setTokenRequestEnhancer(new RequestEnhancer() {

    @Override

    public void enhance(AccessTokenRequest request, OAuth2ProtectedResourceDetails resource,

     

    MultiValueMap<String, String> form, HttpHeaders headers) {

    form.set("appid", resource.getClientId());

    form.set("secret", resource.getClientSecret());

    form.set("grant_type", "client_credential");

    }

    });

    }

     

    }

    OAuth2ClientContext context = new DefaultOAuth2ClientContext();

    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();

    resource.setClientId("xxxxxxxxx");

    resource.setClientSecret("xxxxxxxxxxxxxxx");

    resource.setAccessTokenUri("https://api.weixin.qq.com/cgi-bin/token");

    resource.setAuthenticationScheme(AuthenticationScheme.form);

    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, context);

    restTemplate.setAccessTokenProvider(new WeixinClientCredentialsAccessTokenProvider());

    restTemplate.setRetryBadAccessTokens(true);

    ResponseEntity<Object> response = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/user/get", Object.class);

    if (response.getStatusCode() == HttpStatus.OK) {

    System.out.println(response.getBody());

    }

  • 相关阅读:
    Ubuntu 中软件的安装、卸载以及查看的方法总结
    无锁队列的环形数组实现
    Geodatabase模型
    地图投影与ArcGIS坐标系转换
    ARCGIS动态画点
    ArcEngine数据删除几种方法和性能比较
    AE开发技术文档--8种数据访问方法
    Hibernate 的HQL和sql有什么区别
    ActionContextCleanUp作用
    Hibernate延迟加载与opensessioninviewFilter
  • 原文地址:https://www.cnblogs.com/kingsy/p/6375880.html
Copyright © 2020-2023  润新知