2 回答
TA贡献1934条经验 获得超2个赞
这里TokenRequest
是一个 Spring-bean,它不是一个简单的 java 对象,而是一个具有 spring 特定属性的代理。因此,当您调用authorizationApi.token(tokenRequest)
它时,它会尝试序列化对象并失败,因为它无法序列化特定于 bean 的类(在您的情况下Qualifier
)。
TokenRequest 不应该是 spring 管理的 bean,而是一个简单的 java 对象。因此,删除自动装配并使其成为方法变量实例,而不是保持在类级别。
TokenRequest tokenRequest = new TokenRequest(); tokenRequest.setGrantType(TokenRequest.GrantTypeEnum.PASSWORD); tokenRequest.setUsername(username); tokenRequest.setPassword(password); tokenResponse=authorizationApi.token(tokenRequest); accessToken = tokenResponse.getAccessToken();
TA贡献1828条经验 获得超3个赞
尝试使用以下代码通过 POST 请求从 Web 服务获取令牌。它会起作用的。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public void getHttpCon() throws Exception {
String POST_PARAMS = "grant_type=password&username=someusrname&password=somepswd&scope=profile";
URL obj = new URL("http://someIP/oauth/token");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json;odata=verbose");
con.setRequestProperty("Authorization",
"Basic Base64_encoded_clientId:clientSecret");
con.setRequestProperty("Accept",
"application/x-www-form-urlencoded");
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}
}
添加回答
举报