Working Sample Code for Java?



Does anyone have any working sample code they can share in Java using the new SDK? Not getting anywhere at the minute.

Here’s what I want to do

//Get user’s oauth_token (I’ve got this working)

//Get users’s oauth_verifier (I’ve got this working)

//Get a list of campaigns for this user using the oauth_token and oauth_verifier information or their advertiser account ID or however you get their data

//List these campaigns for the use to see

It’s not clear how/where the different bits of information are used for us as a platform VS the user’s data. Specifically where/when is all of the following used;

  • .setOAuthConsumerSecret("—") (API Account)
  • .setOAuthConsumerKey("—") (API Account)
  • .setOAuthAccessToken("—") (API Account)
  • .setOAuthAccessTokenSecret("—") (API Account)
  • oauth_token (User’s Ad Account)
  • oauth_verifier (User’s Ad Account)

Once I get this basic structure working then hopefully I figure the rest out.


I’ve no idea why this discussion was closed. The solution provided by @JBabichJapan isn’t a full solution.

We’re looking to integrate the Twitter Ads API into a platform - so not a single account. Thus hard coding the account ID into the code isn’t going to work.

How do we get this information programatically? Other than our API Account data, everything else needs to be fully API driven for this to work.

Does anyone have any sample code for that?



Actually, one of the benefits of this library being built on top of twitter4j, pretty much the most popular library for Java support of our REST API, is that there is a lot of documentation online for how twitter4j works and this one was built to be close to that design (it’s more similar to ‘old school’ Java but well tested and stable core library).

The easiest way of understanding an example of how to manage multiple tokens is to authorize several users to use the debugging tool twurl. When you do so, a file called .twurlrc will be populated with those details, so your job is to mimic similar behavior to picking and choosing which user to OAuth as and storing those credentials somewhere (usually secure storage).

You can find some examples online of switching between different users with twitter4j like

You probably figured this out, but there are multiple ways of doing the auth setup, the most basic one is to just use Config Builder but there are also environment variables. It depends on how you want to set your environment up and whether you want to run on a server or local instance. One hello world style test program I wrote several months ago was like this:

public static void main(String[] args) {
    ConfigurationBuilder cb = new ConfigurationBuilder();

    TwitterAdsFactory twitterAdsFactory = new twitter4j.TwitterAdsFactory(;
    TwitterAds twitterAds = twitterAdsFactory.getAdsInstance();
    TwitterAdsCampaignApi campaigns = twitterAds.getCampaignApi();
    String accountID = "<insert your account ID>";

    try {
        BaseAdsListResponseIterable<Campaign> myCampaigns = campaigns.getAllCampaigns(accountID, null, null, false, 10, null, null);
        for (BaseAdsListResponse<Campaign> campaign : myCampaigns){
           System.out.println("name = " + campaign.getName());
    } catch (TwitterException te) {

Hope this helps!