Search code examples
jsonapitwitteroauthtwitter-oauth

Twitter OAUTH Acces Token Response in JSON


I'm using Twitter's REST API to authorize an app. I just authorized the app with

https://api.twitter.com/oauth/authorize

and got an oauth_verifier from the redirect uri.

Then I just used

https://api.twitter.com/oauth/access_token

to get an access_token.

The problem is that the format of response I got from this method is text/plain. It's just like

oauth_token=783897656863051776-5hQfcJTv4FzFpPcyVRbnjPUbJXR29&oauth_token_secret=MViTPOOCaHNZrW6HTIFZ340bq1rutJKL8oqkBxRp2aiW&user_id=783897656863051776&screen_name=sWest&x_auth_expires=0

My query is that how to get the response in application/json format. Any help will be appreciated !!!


Solution

  • You will need to parse it like application/x-www-form-urlencoded content, any language with HTTP Client libraries should provide this functionality. But it isn't clear what language and libraries you are using.

    This example uses joauth library to parse

      protected static Map<String, String> parseTokenMap(String tokenDetails) {
        KeyValueHandler.SingleKeyValueHandler handler =
            new KeyValueHandler.SingleKeyValueHandler();
    
        KeyValueParser.StandardKeyValueParser bodyParser =
            new KeyValueParser.StandardKeyValueParser("&", "=");
        bodyParser.parse(tokenDetails, Collections.singletonList(handler));
    
        return handler.toMap();
      }