2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
這里TokenRequest
是一個(gè) Spring-bean,它不是一個(gè)簡(jiǎn)單的 java 對(duì)象,而是一個(gè)具有 spring 特定屬性的代理。因此,當(dāng)您調(diào)用authorizationApi.token(tokenRequest)
它時(shí),它會(huì)嘗試序列化對(duì)象并失敗,因?yàn)樗鼰o(wú)法序列化特定于 bean 的類(在您的情況下Qualifier
)。
TokenRequest 不應(yīng)該是 spring 管理的 bean,而是一個(gè)簡(jiǎn)單的 java 對(duì)象。因此,刪除自動(dòng)裝配并使其成為方法變量實(shí)例,而不是保持在類級(jí)別。
TokenRequest tokenRequest = new TokenRequest(); tokenRequest.setGrantType(TokenRequest.GrantTypeEnum.PASSWORD); tokenRequest.setUsername(username); tokenRequest.setPassword(password); tokenResponse=authorizationApi.token(tokenRequest); accessToken = tokenResponse.getAccessToken();

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
嘗試使用以下代碼通過(guò) POST 請(qǐng)求從 Web 服務(wù)獲取令牌。它會(huì)起作用的。
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");
}
}
添加回答
舉報(bào)