skip to Main Content

I am trying to make a request to Tweeter with OAuth1 authentication method in Python I manage to do it with requests library but on Java I use Rest assured. Following code does not work, its return Unauthorized :

 Response response = given()
            .headers(headerEmpty())
            .auth()
            .oauth(twitterSettings.getApiKey(), twitterSettings.getApiKeySecret(),
                    twitterSettings.getAccessToken(), twitterSettings.getAccessTokenSecret())
            .spec(baseUrl)
            .get("users/me")
            .then()
            .extract().response();

I look online and I found I need to use probably Scribejava library but not sure how to combine with Rest Assured I am not lazy but some help.

EDIT:
I add the dependencies for Scribe and still does not work.

public static Headers headerEmpty() {
        Header contentType = new Header("Content-Type", "application/json");
        Header acceptType = new Header("Accept", "*/*");

        List<Header> headerList = new ArrayList<>();
        headerList.add(contentType);
        headerList.add(acceptType);

        return new Headers(headerList);
    } 

2

Answers


  1. I think the problem is because of the missing header.
    Try adding this header to the request above:

    Content-Type: application/x-www-form-urlencoded
    

    In order to see exactly the headers you are passing you can change your code like this:

     Response response = given()
                .log().all()
                .header("Content-Type", "application/x-www-form-urlencoded")
                .auth()
                .oauth(twitterSettings.getApiKey(), twitterSettings.getApiKeySecret(),
                        twitterSettings.getAccessToken(), twitterSettings.getAccessTokenSecret())
                .spec(baseUrl)
                .get("users/me")
                .then()
                .extract().response();
    
    Login or Signup to reply.
  2. I came up on similar problem with Rest assured and OAuth1.
    I resolved problem in this way:

    String oauthHeader = "OAuth oauth_consumer_key="" + consumerKey + "", "
                    + "oauth_token="" + accessToken + "", "
                    + "oauth_signature_method="PLAINTEXT", "
                    + "oauth_signature="" + consumerSecret + "%26" + accessTokenSecret + """;
    
        given()
                .contentType(ContentType.JSON)
                .header("Authorization", oauthHeader)
                .body("xyz")
                .when()
                .get("https://xyz")
                .getBody()
                .prettyPrint();
    

    I had a problem to set PLAINTEXT as signature_method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search