oAuth2.0 - password授权方式获取Access token
1.在服务管理中添加oAuth2.0认证
2.配置OAuth Client Options
温馨提示: 此处的客户端ID可以为重定向地址
,也可以指定其它名称。注意:当指定其它名称时调用时则必须在请求header头中设置 service
or X-service
属性,value为上一步的重定向地址。
3.调用OAuth2.0的password接口
java代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.demo</groupId>
<artifactId>DemoCas</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--上边介绍的的版本号等等 将下面的代码放到dependencies标签中即可-->
<dependencies>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
</project>
public static String requestOCRForHttp(String url, Map<String, String> requestParams) throws Exception {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
Iterator<Map.Entry<String, String>> it = requestParams.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> en = it.next();
String key = en.getKey();
String value = en.getValue();
if (value != null) {
params.add(new BasicNameValuePair(key, value));
}
}
// TODO 当配置的 client_id 不是 http://www.baidu.com 时,需在header中添加X-service 或 service属性
//httpPost.setHeader("X-service", "http://www.baidu.com");
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity, "utf-8");
EntityUtils.consume(httpEntity);
} finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
System.out.println("## release resouce error ##" + e);
}
}
return result;
}
public static void main(String[] args) throws Exception {
Map map = new HashMap();
// 指定授权类型为 password
map.put("grant_type", "password");
// TODO 当配置的 client_id 不是 http://www.baidu.com 时,需在header中添加X-service 或 service属性
map.put("client_id", "http://www.baidu.com");
map.put("client_secret", "123456");
map.put("username", "admin");
map.put("password", "111111");
String result = Test.requestOCRForHttp("https://cas.demo.mtn/oauth2.0/accessToken", map);
System.out.println(result);
}