【发布时间】:2016-09-11 11:57:28
【问题描述】:
您好,我正在我的项目中实现 Spring Oauth 2 框架,我在请求访问令牌时收到 401 未经授权的错误,下面是我的代码。
public class Test {
public static void main(String[] args) {
RestTemplate restTemplate=new RestTemplate();
Map<String, String> map=new HashMap<String, String>();
map.put("grant_type", "password");
map.put("client_id", "test");
map.put("client_secret", "test");
map.put("username", "test");
map.put("password", "test");
String url="http://localhost:8080/SpringOauthServer/oauth/token?grant_type={grant_type}&client_id={client_id}&client_secret={client_secret}&username={username}&password={password}";
OauthToken result=restTemplate.getForObject(url, OauthToken.class,map);
System.out.println(result.getAccess_token());
}
}
但是当我使用下面的 curl 命令时,我得到了访问令牌。请帮助我在哪里弄错了..
curl test:test@localhost:8080/SpringOauthServer/oauth/token -d grant_type=password -d client_id=test -d client_secret=test -d username=test -d password=test
回复:
{
"access_token":"d83a312b-323a-40a9-bfc4-c431c40f2ca8",
"token_type":"bearer",
"refresh_token":"17976f94-f3b7-4e2d-8726-3d094f7b1061",
"expires_in":43190,
"scope":"read write trust"
}
【问题讨论】: