Twitter API access error


#1

Hi,

I am new to Twitter API. I am using below code to connect to Ads API. Getting authentication error.

401:Authorization Required
{“errors”:[{“code”:“UNAUTHORIZED_ACCESS”,“message”:“This request is not properly authenticated”}],“request”:{“params”:{}}}

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;

public class Tw {

  static String AccessToken = "asdfasdfa";
  static String AccessSecret = "erqwerqwerqwerqwer";
  static String ConsumerKey = "erqwerqwerqwer";
  static String ConsumerSecret = "erqwerqwerq";

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
	OAuthConsumer consumer = new CommonsHttpOAuthConsumer(
            ConsumerKey,
            ConsumerSecret);

    consumer.setTokenWithSecret(AccessToken, AccessSecret);
  
    
     HttpGet request = new HttpGet("https://ads-api.twitter.com/0/accounts/hkk5/funding_instruments?with_deleted=true&funding_instrument_ids=hw6ie");
    consumer.sign(request);

      HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(request);


    int statusCode = response.getStatusLine().getStatusCode();
    
      System.out.println(statusCode + ":" + response.getStatusLine().getReasonPhrase());
     System.out.println(IOUtils.toString(response.getEntity().getContent()));
}

}

I appreciate your help.


#2

A 401 response implies that there is an issue with how you’re using OAuth in the request.

Are you sure that the app you are working with has access to the Ads API?


#3

Hi,

Thanks for your response. I had issues with Tokens when i created application last month. Twitter team said it was because the application was not white listed. The application is whitelisted on may7th. I got new tokens. I tried above code with these tokens. Not sure why i am still getting this error.

I noticed there is call back URL while creating application. What value do i need to provide in it. As we are not using web application i left it blank.