gpt4 book ai didi

java - OAuth token 请求 - 从 responseentity 获取 access_token

转载 作者:搜寻专家 更新时间:2023-10-31 20:00:13 24 4
gpt4 key购买 nike

public String requestAccessToken(String username, String password, String oauthaurl) {
log.info("Request access token");
String token = null;
List<HttpMessageConverter<?>> converters = getMessageConverters();

RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(converters);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> oauthPayload = new LinkedMultiValueMap<String, String>();
oauthPayload.add("username", username);
oauthPayload.add("password", password);
oauthPayload.add("grant_type", "password");

HttpEntity<?> request = new HttpEntity<>(oauthPayload, headers);
ResponseEntity<?> response = restTemplate.postForEntity(oauthaurl, request, Object.class);
log.info(response.getStatusCode().toString());
log.info(response.getBody().toString());

JSONObject jsonObject = new JSONObject(response.getBody());
log.info(jsonObject.toString());

//get access_token from jsonObject here

return token;

}

我在使用从 token URL 请求 OAuth 访问 token 的方法时遇到问题。我实际上是从带有 access_token 的服务器返回响应,我在解析响应时遇到了问题。

log.info(response.getBody().toString());

返回

{access_token=EI79TeB_Vavu-vgl5WLp8CNWpLUWQBpS3Cm4uU-C1VCJkQJFEo2SthMjPN4gmvQ9ZpQu6Iku11mnvZSUKPVim0MsAEpcMbBdIWl3AMiXCfv9OeqfJRK_1wwIyjo5brDeHK8L5XJPsg98mEKL41qg2IW0Ks9TeYbkRyw4CFKwjcuTi3W3toLBlEGOEinNnrrj6bhOjwaaCVT7zWAIVoImXa-h0VTsoCn2XRVoCO7GENV-Qx55JzTFPJhe2sg72HgRbN8kTID_AcsN8wSKRTQ3T0N74Ks8dfs3YRx0NP4-ADByMcMEnyP8IGoCPHwANNwA8JpYaL2pijWBjOm7VSA53B9Knqxv1EYajBFYXfy74jYUFqlGTKLRUtuKomJh_d9OHM04V-q7xgtlg9upB3s9ORXjbTmzRDzq9U4P67FGJdQe4D4WKUju7oNtjkDzbQZEp0A9fxyHxHFI7MRP4mwPuxMldgytX8Oc1SqoakFre7qzxldaitWqKqnt217e7N7G, token_type=bearer, expires_in=1209599, userName=xxx, .issued=Thu, 02 Mar 2017 16:03:08 GMT, .expires=Thu, 16 Mar 2017 16:03:08 GMT}

我想做的是将响应主体转换为 JSONObject,这样我就可以将 access_token 作为字符串获取。

JSONObject jsonObject = new JSONObject(response.getBody());
log.info(jsonObject.toString());

好像不行——结果是

{}

有人能指出我正确的方向吗?谢谢!

编辑:

编码器 - 当我替换这些行时,我得到如下堆栈跟踪

 2017-03-02 11:16:00.040  INFO 99914 --- [           main] c.u.e.service.EventRecor
at [Source: java.io.PushbackInputStream@1a38c59b; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:60)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2922)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:237)
... 18 more

Coder - 尝试你的最新版本,我得到了这个堆栈跟踪

2017-03-02 11:16:00.040  INFO 99914 --- [           main] c.u.e.service.EventRecor
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:628)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:549)
at com.comp.eventrecorder.service.EventRecorderServiceImpl.requestAccessToken(EventRecorderServiceImpl.java:182)
at com.comp.eventrecorder.service.EventRecorderServiceImpl.startProcessing(EventRecorderServiceImpl.java:57)
at com.comp.eventrecorder.Application.main(Application.java:32)
... 8 more

Coder - Postman fyi 的请求有效

enter image description here

最佳答案

注意:我可能会以错误的方式指向您。请保留您的初始代码并尝试此操作

JSONObject jsonObject = new JSONObject(response.getBody().toString());
log.info(jsonObject.getString("access_token"));

初始答案:

问题似乎是您正在使用 Object 类检索响应实体。修改为string类,让你rest call如下:

ResponseEntity<String> response = restTemplate.postForEntity(oauthaurl, request, String.class);

JSONObject jsonObject = new JSONObject(response.getBody());

log.info(jsonObject.getString("access_token"));

编辑

根据堆栈跟踪,您可能不得不在使用 MultiValueMapLinkedMultiValueMap 时使用自定义实现。而不是像下面那样修改您的代码。

public String requestAccessToken(String username, String password, String oauthaurl) {
log.info("Request access token");
String token = null;

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);



UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(oauthaurl)
.queryParam("grant_type", "password")
.queryParam("username", username)
.queryParam("password", password);

URI myUri=builder.buildAndExpand().toUri();

HttpEntity<?> request = new HttpEntity<>(headers);
ResponseEntity<String> rs = restTemplate.exchange(myUri, HttpMethod.POST, request,String.class);
JSONObject jsonObject = new JSONObject(rs.getBody());
log.info(jsonObject.getString("access_token"));

//get access_token from jsonObject here

return token;

}

如果您需要澄清,请告诉我!!

关于java - OAuth token 请求 - 从 responseentity 获取 access_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42560378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com