简体   繁体   English

HTTP 状态 - 401 未经授权

[英]HTTP Status - 401 Unauthorized

I'm trying to convert from curl to java code with intention to call an API to create a token but I get a 401 response.我正在尝试从 curl 转换为 java 代码,意图调用 API 来创建令牌,但我得到 401 响应。

Curl code: Curl 代码:

curl -X POST \
  https://apigw.dev/commercial/oauth/create-token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'client_id=dfdfdfdfd&client_secret=dfdfdf&grant_type=client_credentials'

And here my java code:这里是我的 java 代码:

@RestController
public class TokenController {

    @RequestMapping(value = "/getAccessToken", method = RequestMethod.POST)
    public ClientCredentialsResourceDetails getAccessToken(ClientCredentialsResourceDetails resource) throws Exception {

        resource.setAccessTokenUri(tokenUri);
        resource.setClientId(clientId);
        resource.setClientSecret(clientSecret);
        resource.setGrantType("client_credentials");
        resource.setClientAuthenticationScheme(AuthenticationScheme.header);
        resource.setScope(Arrays.asList("read", "write"));

        System.out.println(resource);

        return resource;
    }
}

and here the results:结果如下:

    "timestamp": "2020-04-28T14:02:28.381+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/getAccessToken"
}

I really appreciate your suggestion, Thanks in advance.非常感谢您的建议,在此先感谢。

You're making a POST request with CURL, but your rest controller is configured as GET.您正在使用 CURL 发出 POST 请求,但您的 rest controller 配置为 GET。 I guess you have another endpoint to /getAccessToken with POST method wich requires the token.我猜你有另一个使用 POST 方法的 /getAccessToken 端点需要令牌。 Also, by default, every mehod in a @RestController class will consume an application/json and produce an application/json .此外,默认情况下, @RestController class 中的每个方法都将使用application/json并生成application/json If your request is a Content-Type: application/x-www-form-urlencoded , change the consume type of your request to MediaType.APPLICATION_FORM_URLENCODED_VALUE .如果您的请求是Content-Type: application/x-www-form-urlencoded ,请将请求的使用类型更改为MediaType.APPLICATION_FORM_URLENCODED_VALUE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot 为简单的获取请求获取 401 未经授权的状态代码 - Spring boot getting 401 unauthorized status code for simple get request Spring Cloud Dataflow应用程序HTTP返回未经授权的401 - Spring cloud dataflow app http returns 401 unauthorized Spring Social Twitter流返回未经授权的HTTP 401 - Spring Social Twitter stream returns HTTP 401 unauthorized HTTP 401 在 Spring 启动测试中出现未经授权的错误 - HTTP 401 Unauthorized error occurs in Spring Boot test 如何拦截角度为2的HTTP状态代码401? - How to intercept http status code 401 with angular 2? 401未经授权的错误角度5 - 401 Unauthorized Error Angular 5 Java 异常:未经授权:401 - Java Exception: Unauthorized: 401 Java Spring 401 未授权 - Java Spring 401 Unauthorized 状态=401,错误=未授权,消息=在尝试接收令牌时需要完全身份验证才能在 Spring 框架中访问此资源 - status=401, error=Unauthorized, message=Full authentication is required to access this resource in Spring Framework when trying to receive a token 带有 Spring Boot 和 Mysql 的 JWT,得到错误“status”:401,“error”:“Unauthorized”,同时提供访问角色 - JWT with Spring Boot and Mysql, getting an error "status": 401, "error": "Unauthorized", while giving an access roles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM