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:

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 =
newAPI(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.

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:

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

// 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 =
newAPI(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 = newTable();

Literal l = newLiteral();

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

form1.Controls.Add(l);

Literal l2 = newLiteral();

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

form1.Controls.Add(l2);

TableRow row =
newTableRow();

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.