Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter oauth2 using javax.ws.rs

I have this request for twitter using javax.ws.rs

    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 are my consumer secret and consumer key encoded in base 64.

The request I'm trying to do is:

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

I keep getting 403 Forbidden: {"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}

It seems that the post body isn't set correctly, anyone know how to set it?

like image 258
Jones Avatar asked Oct 31 '22 11:10

Jones


1 Answers

What you could try is to change the content type of the POST request body/entity like this:

 .post(Entity.entity("grant_type=client_credentials",  MediaType.APPLICATION_FORM_URLENCODED)
like image 92
FrAn Avatar answered Nov 10 '22 00:11

FrAn