Twitter oauth2使用javax.ws.rs

我有使用
javax.ws.rs的twitter请求

    WebTarget target = new WebTargetBuilder(client, OAUTH_API_ENDPOINT).build();

    Builder request = target
            .request(MediaType.APPLICATION_JSON)
            .header("Authorization", "Basic " + encodedCredentials)
            .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    Response postResponse = request
            .post(Entity.entity("grant_type=client_credentials", MediaType.TEXT_PLAIN));

    System.out.println(postResponse.readEntity(String.class));

encodedCredentials是我的消费者密钥和基础64中编码的消费者密钥.

我正在尝试做的请求是:

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
                 NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==Content-Type: application/x-www-   form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

我不断收到403禁止:{ “错误”:[{ “代码”:170, “消息”: “缺少必需的参数:grant_type”, “标签”: “forbidden_​​missing_parameter”}]}

似乎帖子体设置不正确,有谁知道如何设置它?

最佳答案 您可以尝试更改POST请求正文/实体的内容类型,如下所示:

 .post(Entity.entity("grant_type=client_credentials",  MediaType.APPLICATION_FORM_URLENCODED)
点赞