Twitter API :Your credentials do not allow access to this resource :code:220


#1

HI,

indent preformatted text by 4 spaces

import java.awt.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

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;
import org.json.JSONObject;

public class TwitterNew {

static String accessTokenStr ="********************************";
static String accessTokenSecretStr ="********************************";
static String consumerKeyStr = "********************************";
static String consumerSecretStr ="********************************";

indent preformatted text by 4 spaces

public static void main(String[] args) {
	try{
	String url="https://api.twitter.com/1.1/users/search.json?q=sony&count=2";
	OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKeyStr,
			consumerSecretStr);
	oAuthConsumer.setTokenWithSecret(accessTokenStr, accessTokenSecretStr);

// oAuthConsumer.
HttpGet httpPost = new HttpGet(url);

	oAuthConsumer.sign(httpPost);
	//oAuthConsumer.//

System.out.println("the url is for request "+httpPost.getURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
//

System.out.println(IOUtils.toString(httpResponse.getEntity().getContent()));
	BufferedReader streamReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); 
	StringBuilder responseStrBuilder = new StringBuilder();
    String inputStr;
    while ((inputStr = streamReader.readLine()) != null)
    responseStrBuilder.append(inputStr);
    System.out.println(""+responseStrBuilder.toString());
    JSONArray responseJson=new JSONArray(responseStrBuilder.toString());
    System.out.println("The Content is TwitterJson text is  "+responseJson);
    JSONObject  TwitterJson=responseJson.getJSONObject(0);
    System.out.println("The Content is TwitterJson text is  "+TwitterJson);
 

indent preformatted text by 4 spaces
    
    JSONObject  recenttweetJson=TwitterJson.getJSONObject("status");
    
    
    JSONObject  entities=recenttweetJson.getJSONObject("entities");
    JSONArray   media=entities.getJSONArray("media");
    JSONObject  subMedia=media.getJSONObject(0);
    String urlHttps=subMedia.getString("media_url_https");
    String urlHttp=subMedia.getString("media_url");
    long id=recenttweetJson.getLong("id");
    		/*media_url_https*/
    HttpGet  httpget=new HttpGet(urlHttps);
	HttpClient httpClient2 = new DefaultHttpClient();
	HttpResponse httpResponse2 = httpClient2.execute(httpget);
	int statusCode = httpResponse2.getStatusLine().getStatusCode();
	
	

System.out.println("The status code og image url is"+statusCode);
	if(statusCode==200){
		Image image = null;
		URL surl = new URL(urlHttps);
        image = ImageIO.read(surl);
		
		JFrame frame = new JFrame();
	
        frame.setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        frame.add(label);
        frame.setVisible(true);
        }
	}catch(Exception e){
		System.out.println("Exception in TIWTTER API");
		e.printStackTrace();
	}
}
indent preformatted text by 4 spaces

protected static String getTimestampFromLocalTime(Date date) {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    return format.format(date);
}
}
indent preformatted text by 4 spaces

Response:
{“errors”:[{“message”:“Your credentials do not allow access to this resource”,“code”:220}]}

How to use the API with out any interrupts ,Suggest Me


#2

Please indent each line of your code with 4 spaces so it is actually readable…