javakotlintwitterscribe

Making Twitter OAuth1 PUT request with JSON body with scribejava


I am trying to call this twitter v2 endpoint to hide a tweet using OAuth1 and ScribeJava

Here is what I have tried

val service = ServiceBuilder(apiKey)
        .apiSecret(apiSecret)
        .build(TwitterApi.instance())

    val url = "https://api.twitter.com/2/tweets/${tweetId}/hidden"

    val oauth1 = OAuth1AccessToken(token,secret)
    val request = OAuthRequest(Verb.PUT, url)
    request.setPayload("{ \"hidden\": true }")

    service.signRequest(oauth1, request)
    val response = service.execute(request)

When I try that I get a 400 Bad Request back, what is the proper way to do this?


Solution

  • The reason of your problem probably has to do with the fact that you are trying to hide a tweet that you do not have the ability to do so. Please, note the restrictions that a tweet must adhere to in order to be hidden. It is stated in the section Step three: Find a Tweet ID to hide in the Twitter developer documentation:

    The hide replies endpoint can hide or unhide replies on behalf of an authorized user. Because we are using the Access Tokens related to your user profile in this example, you will be able to hide replies from users who participate in a conversation started by you. Similarly, if you were using Access Tokens that belong to another user that authorized your app, you would be able to moderate replies to any conversations started by that account.

    Ask a friend to reply to a Tweet (let them know you're testing hide replies) or reply to any of your Tweets from a test account. Click on that reply, then copy the numeric part of its URL. That will be the Tweet ID we will hide.