If you are planning on writing an application using the AWeber API in .NET, then getting started is a breeze using the AWeber .NET API Library. This tutorial will walk you through building your first app using the .NET library, and break down everything you need to know to get up and running quickly with the AWeber API.

Requirements

First off, this walkthrough assumes you have a few things under your belt already. To be able to complete this walkthrough, you will need:

  • A free developer account. Don't have one yet? Sign up here 
  • An app created in your labs account. (Have your keys handy)
  • A working knowledge of .NET 4.0+ / HTML
  • A copy of the AWeber API .NET Library

Step 1: Creating Your Application

This following example assumes you are using a standard ASP.NET. You can modify this for an MVC, WPF or Winforms application. First you must reference the Aweber.dll. On your project right click References and press Add Reference. Select the Aweber.dll and press OK.

At the top of every class you may add the following using statements for ease of use.

using Aweber;

using Aweber.Entity;

Next we will create an instance of the Aweber API.

String consumerKey = "vjckuVc3sNrZWOa3PWnf";

String consumerSecret = "u3sQ7vGGJZPCKhgEOAXeqgBSi685c5x2wm6gZuIj";

 

// Create a new api instance 

API api = new API(consumerKey, consumerSecret);

Be sure to replace the consumerKey and consumerSecret in this example with the keys of your application. These keys are listed under your applications on the My Apps page in your AWeber Labs developer control panel. Remember, do not ever share or distribute your application's key and secret. If you plan on distributing your application, whether as a desktop, mobile, or open source application, please read about the other forms of authentication AWeber provides. 

Next, we will need to make an instance of the AWeberAPI class. This class will be how you connect to AWeber's API. Each instance of this class is tied to a specific application. This is done by including the consumer key and consumer secret of your app as arguments when creating a new instance. We now have an AWeberAPI object that we can use to connect to specific users' AWeber accounts.

Step 2: Getting a request token and authorizing a user

In order to do anything for an AWeber user, that user must first grant your application permission to access his data. The AWeber API uses the OAuth authorization model. In a nutshell, users will never have to give out their passwords to your application, or any other application. Instead, AWeber will give your application "tokens", which are random letters and numbers, that your application can use as magic keys to access that specific user's AWeber data on her behalf. The AWeber API .NET Library takes care of all of this for you.

// Set callback url (if not set will default to this page) 

api.CallbackUrl = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Authorize.aspx";

 

// Get request token

api.get_request_token();

 

// Save the token and secret in session

HttpContext.Current.Session.Add("oauth_token", api.OAuthToken);

HttpContext.Current.Session.Add("oauth_token_secret", api.OAuthTokenSecret);

 

// Will redirect user to the Aweber authorize page

api.authorize();

Here, we are assuming we are authenticating a user for the first time, so we need to get a request token. This is the first step in getting the tokens we need for a user. To get a request token, we need to give the AWeberAPI object a callback URL. This is the URL where AWeber will send the tokens that it generates for this user to use your application. In this example, we are using .NET to give AWeber the URL of another page called Authorize.aspx. In exchange, the AWeber API gives us back two tokens - the request token, and the request token secret.

We don't need the token secret just yet, so we are going to save it in a session on the user's computer so we can get to it later. First, we need to get the user's permission to access their data. This is done by redirecting them to the authorize URL, with the request token in the query string.


 

Step 3: Getting permission from the user

 

The customer will then be redirected to a special page on AWeber's site where they can grant permission to your application. Since you included the request token in the URL, we are able to tell which app this customer is granting permission to, and where to send the user when they complete this process.

The customer can now enter their AWeber username and password and grant permission to your application to access their data. The customer will then be sent back to the URL you provided earlier, which in our case was the exact same URL they went to start the process. The only difference is that the URL will have some special data appended to the end of it. Depending on where your application is running, the URL may look something like this:

http://localhost/Authorize.aspx?oauth_verifier=gl6z1j&oauth_token=BzB8Tfhw1c39Zp2AHU4Oga6AuLZ7r4xgQWnPCCn2

The URL now has the request token you used to get authorization from the customer sent back as the oauth_token, as well as a new piece of data called the oauth_verifier. Your app can now use these pieces of data to gain access to the user's AWeber account.


 

Step 4: Gaining access to the customer's data

// Replace with the keys of your application

// NEVER SHARE OR DISTRIBUTE YOUR APPLICATIONS'S KEYS!

// It is recommended you place these within your web.config application settings

String consumerKey = "vjckuVc3sNrZWOa3PWnf";

String consumerSecret = "u3sQ7vGGJZPCKhgEOAXeqgBSi685c5x2wm6gZuIj";

 

// Create a new api instance

API api = new API(consumerKey, consumerSecret);

 

// Fill the tokens back from user session

api.OAuthToken = Convert.ToString(Session["oauth_token"]);

api.OAuthTokenSecret = Convert.ToString(Session["oauth_token_secret"]);

 

// Get the returned oauth_verifier

api.OAuthVerifier = Request["oauth_verifier"];

 

// Get Access Token (this is the permanent token to be stored for future access)

api.get_access_token();

 

 

As we go to Authorize.aspx we must first create the API instance again and insert the consumerKey, consumerSecret, oauth_token and oauth_token_secret we stored earlier.

Now we can use the token secret we saved before, along with the request token and verifier to gain access to this customer's account. Calling getAccessToken() will use these pieces of data to get a final pair of token: the access token, and its token secret. If your application intends to allow users to access their data repeatedly, you will want to save their access token and token secret in some manner, such as in a database. These are the only two pieces of data (along with your consumer key / secret) that you need to access this particular customer's data through the API. In this example, we are simply storing this data in the user's session, however it would probably be more common to see this information stored on your server such as in a database.

We issue a redirect to clear the user's URL, and are now able to access the customer's information by using the data we have stored in their cookies. If your application saves these tokens, allowing users to return to your app without re-authorizing it, you can gain access to an account by skipping Steps 2 and 3, and simply doing:

 

// Create a new api instance

API api = new API(consumerKey, consumerSecret);

 

api.OAuthToken = "Previously Stored Token";


 

Step 5: Make something!

Great, now that we can access a user's AWeber data through the API, we can get to actually writing an app. For this basic app, we are just going to print some basic broadcast statistics for the user. Everything in the AWeber API is either an entry, which is one single instance of something (such as an account, or a list, or a web form), or a collection of entries.

Working from our account object, we can access a collection of all of their lists, and loop through them.

// Get your account

Account account = api.getAccount();

 

// Loop through all lists in your account

foreach (Entity.List list in account.lists().entries)

{

      // Output list values to a table

      Table t = new Table();

 

      Literal l = new Literal();

      l.Text = String.Format("<h1>List: {0}</h1>", list.name);

      form1.Controls.Add(l);

 

      Literal l2 = new Literal();

      l2.Text = String.Format("<h3>{0}</h3>", list.id);

      form1.Controls.Add(l2);

 

      TableRow row = new TableRow();

 

                row.Cells.Add(new TableCell() { Text = "Subject" });

      row.Cells.Add(new TableCell() { Text = "Sent" });

      row.Cells.Add(new TableCell() { Text = "Stats" });

 

      t.Rows.Add(row);

Here, we just print the list name then build a basic table. Each list entry has a collection of campaigns, so let's loop through them and print stats about each of our broadcasts.

// Loop through all campaigns in your list

foreach (Campaign campaign in list.campaigns().entries)

{

 

       if (campaign.type == "broadcast_campaign")

       {

             TableRow campaignRow = new TableRow();

 

             campaignRow.Cells.Add(new TableCell() { Text = String.Format("<em>{0}</em>", campaign.subject) });

 

             campaignRow.Cells.Add(new TableCell() { Text = campaign.sent_at.Value.ToString("MMM dd, YYYY hh:mm t") });

 

             campaignRow.Cells.Add(new TableCell() { Text = String.Format("<ul><li><b>Opened:</b> {0}</li><li><b>Sent:</b> {1}</li><li><b>Clicked:</b> {2}</li></ul>", campaign.total_opens, campaign.total_sent, campaign.total_clicks) });

 

              t.Rows.Add(campaignRow);

         }

 

}

 

        // Add table to page output

        form1.Controls.Add(t);

}

 

And that's it! Every type of entry in the AWeber API has its own unique set of attributes, and its own set of child collections.

For more information on what entries are supported in the API and what properties they have, read up on our full reference documentation.

Last edited Apr 5, 2012 at 2:14 AM by Adam, version 1

Comments

roysubham Feb 26, 2016 at 8:19 AM 
Hi ,Is this library is not supportable for windows application , i tried but that does not working , can u please provide the source code for windows form , thank you .

AjayKelkar Jul 8, 2012 at 8:03 PM 
Hi ,

Nice Work ! . The library helped a lot. One question I have , how to add new subscriber ?

aweberClient = new API(consumerKey, consumerSecret);
aweberClient.OAuthToken = "< my token >" l
aweberClient.OAuthTokenSecret = "< my secret token >"

Aweber.Entity.Subscriber subscriber = new Aweber.Entity.Subscriber(aweberClient.adapter);
subscriber.name = name;
subscriber.email = email;
subscriber.Save()l

I am getting error subscription url missing , is there any property of subscriber object needs to be set ?

Please help , i am stuck.