尝试在Java SpringBoot中使用REST API时收到错误请求

阿迪蒂

嗨,我正在尝试使用POST使用REST端点,但是出现以下错误。端点在POSTMAN中给出了正确的响应,但是在Java中却出现错误。请让我知道错误在哪里。

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{
  "error":"unsupported_grant_type",
  "error_description":"The given grant_type is not supported"
}]] with root cause

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{
  "error":"unsupported_grant_type",
  "error_description":"The given grant_type is not supported"
}]

下面是代码:

控制器:

public class MemberController {

private static final Logger log = LoggerFactory.getLogger(MemberController.class);

@Autowired
MemberService memberService;

@PostMapping(value = "/token",  headers = "Accept=application/json")
public String getToken() {
    log.info("Test getToken method");
    return memberService.callTokenService();
}

服务等级:

public class MemberService {    
    private static final Logger log = LoggerFactory.getLogger(MemberService.class);    
    private RestTemplate restTemplate = new RestTemplate();    
    final String tokenURL = "-------";    

    public String callTokenService() {

        log.info("Inside Service");
    TokenInput input = new TokenInput();
    
    String clientId = "l7xxef159fc30ee8479e9a7dab859c458a4d";
    String clientSecret = "a63d0b4a01b844c0b7e7eb724ef13959";
    String grantType = "client_credentials";
    
    input.setCLIENT_ID(clientId);
    input.setCLIENT_SECRET(clientSecret);
    input.setGRANT_TYPE(grantType);
    

    ResponseEntity<TokenProperties> response = restTemplate.postForEntity(tokenURL,  input, TokenProperties.class);
    HttpStatus status = response.getStatusCode();
    
    log.info("Status: "+status);
    log.info("Response: "+response.toString());

    return response.toString();
    }

    

}

POJO类:

@JsonIgnoreProperties(ignoreUnknown = true)  
public class TokenProperties {    
    String access_token;
    String token_type;
    String expires_in;
    String scope;
    public String getAccess_token()
    {
        return access_token;
    }

    public void setAccess_token(String access_token)
    {
        this.access_token = access_token;
    }

    public String getToken_type()
    {
        return token_type;
    }

    public void setToken_type(String token_type)
    {
        this.token_type = token_type;
    }

    public String getExpires_in()
    {
        return expires_in;
    }
    public void setExpires_in(String expires_in)
    {
        this.expires_in = expires_in;
    }

    public String getScope()
    {
        return scope;
    }

    public void setScope(String scope)
    {
        this.scope = scope;
    }

    @Override
    public String toString() {
        return "{" + "access_token='" + access_token + '\'' + ", token_type=" + token_type + ", expires_in=" + expires_in + '\'' + "scope='" + scope + '}';
    }

}

TokenInput POJO:

package com.memberservice_v2;

公共类TokenInput {

String CLIENT_ID;
String CLIENT_SECRET;
String GRANT_TYPE;

public String getCLIENT_ID() {
    return CLIENT_ID;
}
public void setCLIENT_ID(String cLIENT_ID) {
    CLIENT_ID = cLIENT_ID;
}
public String getCLIENT_SECRET() {
    return CLIENT_SECRET;
}
public void setCLIENT_SECRET(String cLIENT_SECRET) {
    CLIENT_SECRET = cLIENT_SECRET;
}
public String getGRANT_TYPE() {
    return GRANT_TYPE;
}
public void setGRANT_TYPE(String gRANT_TYPE) {
    GRANT_TYPE = gRANT_TYPE;
}

}

有人可以帮我吗?请让我知道错误在哪里。提前致谢!

哈里

ResponseEntity<TokenProperties> response = restTemplate.postForEntity(tokenURL, null, TokenProperties.class);对令牌端点的请求不完整。据我在上述代码中所看到的,您没有传递有效负载(令牌请求)。

首先,创建令牌请求并设置适当的属性,例如客户端ID,授予类型等。

TokenRequest tokenRequest = new TokenRequest();
// set the attributes as per the token endpoint

ResponseEntity<TokenProperties>  response = restTemplate.postForEntity(tokenURL,  tokenRequest, TokenProperties.class);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试链接 API 时收到“400 错误请求”

当我尝试使用Swagger在AngularJS上构建发布请求时收到错误的请求

尝试使用“ http:// localhost:61961 / Token”登录到Web API。收到400错误的请求错误

尝试在ggplot中使用stat_function时收到错误

在Java中收到此错误。错误:尝试使用(+)加法运算符时未声明

尝试连接到REST API时收到有效负载错误

当我尝试通过 REST 请求发送 DM 时,我收到了错误的身份验证数据

使用Microsoft Speech到文本REST API时,收到400- OGG文件格式的错误请求

尝试在HTTPCLIENT -JAVA中发送POST请求时,收到400错误请求

尝试使用Java将PDF上载到Mendeley时收到500错误

尝试使用“请求”模块时导入错误

当我尝试从预定的云函数访问外部 API 时,我不断收到错误消息:错误:找不到模块“请求”

当我尝试通过访存与Api连接时,为什么会收到“错误请求”错误400?

收到(400)错误的请求。尝试发送GCM消息时

尝试发送 POST 请求时收到 404 错误

尝试使用带有有效购买令牌的Java Google Play Developer API v3检索应用内购买信息时,请求错误(值无效)

使用Facebook Graph API时遇到错误-会话:尝试为具有待处理请求的会话请求新权限

尝试使用axios访问Wikipedia Api,但收到CORS政策错误

500内部服务器错误; 在springBoot rest api中使用POST方法时

尝试在 Swift 中使用 API 连接时出现 SSL 错误

尝试使用Java脚本从FLASK API请求数据(POST)

尝试在Java中使用Jackson时出现的问题

IOError:尝试在Java中使用jython时出现FileNotFound

尝试在Java中使用线程写入图像时发生FileNotFoundException

尝试在Java中使用Apache HttpClient进行GET时出错

在Java中使用For循环时尝试/捕获问题

NoClassDefFoundError尝试在Android Studio中使用FatSecret REST API

尝试在Python中使用REST API在Atlassian Bitbucket中创建提取请求

在Java中使用BitBucket REST API