Why Bad Authentication data.for RestAPi


#1

I am trying to search for user profile by using REST API using GET users/search METHOD .Even though i am providing proper
Consumer Key,Secret Key
Access Token,Access Token secret,

The Response is :
400:Bad Request
{“errors”:[{“code”:215,“message”:“Bad Authentication data.”}]}

My Java Programme

import java.io.BufferedReader;
import java.io.InputStreamReader;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
public class TwitterRestAPi {
static String AccessToken =“";
static String AccessSecret =“";
static String ConsumerKey = "
******”;
static String ConsumerSecret ="

public static void main(String[] args) throws Exception {
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(ConsumerKey,ConsumerSecret);
consumer.setTokenWithSecret(AccessToken, AccessSecret);
String FinalUrl="https://api.twitter.com/1.1/users/search.json?q=netelixir&count=1";
System.out.println("FinalUrl"+FinalUrl);
HttpGet  httpget=new HttpGet(FinalUrl);
System.out.println("bfr val"+httpget);
consumer.sign(FinalUrl);
@SuppressWarnings("deprecation")
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpget);

int statusCode = response.getStatusLine().getStatusCode();

System.out.println(statusCode + “:” + response.getStatusLine().getReasonPhrase());

System.out.println(IOUtils.toString(response.getEntity().getContent()));

}

}

#2

Please indent code with 4 spaces, so it is displayed as a codeblock and properly readable.

And it looks like you are passing the URL String to the Signpost OAuth Consumer, while this is possible, it isn’t best practice. Additionally afterwards you are not setting the URL to be used by the actual request. Please try something similar to the following:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;

static String AccessToken     = "********************";
static String AccessSecret    = "****************";
static String ConsumerKey     = "**********************";
static String ConsumerSecret  = "********************"

public static void main(String[] args) throws Exception {
    // Prepare OAuth Consumer
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(ConsumerKey,ConsumerSecret);
    consumer.setTokenWithSecret(AccessToken, AccessSecret);

    // Prepare request
    URL url = new URL("https://api.twitter.com/1.1/users/search.json?q=netelixir&count=1");
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    consumer.sign(request);
    
    // Response
    System.out.println("Sending request to Twitter...");
    request.connect();
    System.out.println("Response: " + request.getResponseCode() + " "
        + request.getResponseMessage());
}

#3

Maybe also relevant: This Stackoverflow question.