使用javax.ws.rs的TwitterOAuth2

使用javax.ws.rs的TwitterOAuth2,java,twitter,oauth,Java,Twitter,Oauth,我使用javax.ws.rs请求twitter WebTarget target = new WebTargetBuilder(client, OAUTH_API_ENDPOINT).build(); Builder request = target .request(MediaType.APPLICATION_JSON) .header("Authorization", "Basic " + encodedCredentials

我使用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是在base 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,“消息”:“缺少必需参数:授予类型”,“标签”:“禁止缺少参数”}]}


似乎post body设置不正确,有人知道如何设置吗?

我在PHP中使用了相同的设置,我认为您遗漏了必需的参数,如oauth_签名

您可以尝试更改post请求body/实体的内容类型,如下所示:

 .post(Entity.entity("grant_type=client_credentials",  MediaType.APPLICATION_FORM_URLENCODED)
  private static final String OAUTH_API_ENDPOINT = "https://api.twitter.com/oauth2/token";
  private String consumerKey = "your consumer key";
  private String consumerSecret = "your consumer secret";

// Constructs the request for requesting a bearer token and returns that
// token as a string
public String requestBearerToken() throws IOException, InterruptedException, ExecutionException {

    String encodedCredentials = encodeCredentials();

    Client client = ClientBuilder.newClient();

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

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

    return postResponse.toString();
}

// Encodes the consumer key and secret to create the basic authorization key
public String encodeCredentials() {
    try {
        String encodedConsumerKey = URLEncoder.encode(consumerKey, "UTF-8");
        String encodedConsumerSecret = URLEncoder.encode(consumerSecret,
                "UTF-8");

        String fullKey = encodedConsumerKey + ":" + encodedConsumerSecret;
        byte[] encodedBytes = Base64.encodeBase64(fullKey.getBytes());
        return new String(encodedBytes);
    } catch (UnsupportedEncodingException e) {
        return new String();
    }
}

因此,从Twitter获得具有消费者密钥和消费者机密的承载令牌的最终代码如下所示:

 .post(Entity.entity("grant_type=client_credentials",  MediaType.APPLICATION_FORM_URLENCODED)
  private static final String OAUTH_API_ENDPOINT = "https://api.twitter.com/oauth2/token";
  private String consumerKey = "your consumer key";
  private String consumerSecret = "your consumer secret";

// Constructs the request for requesting a bearer token and returns that
// token as a string
public String requestBearerToken() throws IOException, InterruptedException, ExecutionException {

    String encodedCredentials = encodeCredentials();

    Client client = ClientBuilder.newClient();

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

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

    return postResponse.toString();
}

// Encodes the consumer key and secret to create the basic authorization key
public String encodeCredentials() {
    try {
        String encodedConsumerKey = URLEncoder.encode(consumerKey, "UTF-8");
        String encodedConsumerSecret = URLEncoder.encode(consumerSecret,
                "UTF-8");

        String fullKey = encodedConsumerKey + ":" + encodedConsumerSecret;
        byte[] encodedBytes = Base64.encodeBase64(fullKey.getBytes());
        return new String(encodedBytes);
    } catch (UnsupportedEncodingException e) {
        return new String();
    }
}