Authentication

These code snippets demonstrate how to connect your app to an AWeber Customer account.
Check out our Getting Started guide if you're not sure how to use these.

Getting a Request Token

This code snippet demonstrates how to get a request token.
This code sample applies to private apps only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
require_once('aweber_api/aweber_api.php');

// Put the consumer key and secret from your App on labs.aweber.com below.
$consumerKey    = '****';
$consumerSecret = '****';

$aweber = new AWeberAPI($consumerKey, $consumerSecret);

// Put the callback URL of your app below or set to 'oob' if your app isnt
// a web based application.
$callbackURL    = 'https://www.example.com/your_application_callback_url';

// get a request token
list($key, $secret) = $aweber->getRequestToken($callbackURL);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from aweber_api import AWeberAPI

consumer_key = "*****"
consumer_secret = "*****"
callback_url = 'https://www.example.com/your_application_callback_url'

aweber = AWeberAPI(consumer_key, consumer_secret)

# get a request token
key, secret = aweber.get_request_token(callback_url)

Authorizing Your App

This code snippet demonstrates how to redirect the AWeber Customer to authorize your app.
This code sample applies to private apps only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
require_once('aweber_api/aweber_api.php');

// Put the consumer key and secret from your App on labs.aweber.com below.
$consumerKey    = '****';
$consumerSecret = '****';

$aweber = new AWeberAPI($consumerKey, $consumerSecret);

// Put the callback URL of your app below or set to 'oob' if your app isnt
// a web based application.
$callbackURL    = 'https://www.example.com/your_application_callback_url';

// get a request token
list($key, $secret) = $aweber->getRequestToken($callbackURL);

// get the authorization URL
$authorizationURL = $aweber->getAuthorizeUrl();

// store the request token secret
setcookie('secret', $secret);

// redirect user to authorization URL
header("Location: $authorizationURL");
exit();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from aweber_api import AWeberAPI
from pylons import session
from pylons.controllers.util import redirect

consumer_key = "*****"
consumer_secret = "*****"
callback_url = 'https://www.example.com/your_application_callback_url'

aweber = AWeberAPI(consumer_key, consumer_secret)

# get a request token
key, secret = aweber.get_request_token(callback_url)

# get the authorization URL
authorization_url = aweber.authorize_url

# store the request token secret
session['secret'] = secret
session.save()

# redirect user to authorization URL: Pylons example
redirect(authorization_url)

Getting an Access Token

This code snippet demonstrates how to exchange a request token + verifier code for an access token.
This code sample applies to private apps only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php
require_once('aweber_api/aweber_api.php');

// Put the consumer key and secret from your App on labs.aweber.com below.
$consumerKey    = '****';
$consumerSecret = '****';

$aweber = new AWeberAPI($consumerKey, $consumerSecret);

// Pull the request token key and verifier code from the URL
$aweber->user->requestToken = $_GET['oauth_token'];
$aweber->user->verifier = $_GET['oauth_verifier'];

// retrieve the stored request token secret
$aweber->user->tokenSecret = $_COOKIE['secret'];

// Exchange a request token with a verifier code for an access token.
list($accessTokenKey, $accessTokenSecret) = $aweber->getAccessToken();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from aweber_api import AWeberAPI
from pylons import session, request

# Put the consumer key and secret from your App on labs.aweber.com below.
consumer_key = '****'
consumer_secret = '****'

aweber = AWeberAPI(consumer_key, consumer_secret)

# Pull the request token key and verifier code from the request
aweber.user.request_token = request.params['oauth_token']
aweber.user.verifier = request.params['oauth_verifier']

# retrieve the stored request token secret
aweber.user.token_secret = session['request_token_secret']

# Exchange a request token with a verifier code for an access token.
access_token, access_secret = aweber.get_access_token()

Authorizing a Distributed App

This code snippet demonstrates how to use the App ID to authorize your public app.
This code sample applies to public apps only.

1
2
3
4
5
6
7
8
9
<?php

// Enter your 'App ID' from your application on labs.aweber.com
$appID = "****";

// Redirect your user to the distributed app authorization URL
$authorizationURL = "https://auth.aweber.com/1.0/oauth/authorize_app/$appID";
header("Location: $authorizationURL");
exit();
1
2
3
4
5
6
7
8
from pylons.controllers.util import redirect

# Enter your 'App ID' from your application on labs.aweber.com
app_id = '****'

# Redirect your user to the distributed app authorization URL
authorization_url = 'https://auth.aweber.com/1.0/oauth/authorize_app/%s'
redirect(authorization_url % app_id)

Using the Authorization Code

This code snippet demonstrates how to exchange your authorization code for an access token.
This code sample applies to public apps only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
# Complete example on how to parse an authorization code.

# Refer to our getting started guide for a complete API walkthrough
# https://labs.aweber.com/snippets/gs/using_the_api

require_once('aweber_api/aweber_api.php');

try {
    # set $authorization_code to the code that is given to you from
    # https://auth.aweber.com/1.0/oauth/authorize_app/YOUR_APP_ID
    $authorization_code = "Az12...|234k...|Aq53...|ka4l...|aj3ksf|";

    $auth = AWeberAPI::getDataFromAweberID($authorization_code);
    list($consumerKey, $consumerSecret, $accessKey, $accessSecret) = $auth;

    # Store the Consumer key/secret, as well as the AccessToken key/secret
    # in your app, these are the credentials you need to access the API.
}
catch(AWeberAPIException $exc) {
    print "<h3>AWeberAPIException:</h3>";
    print " <li> Type: $exc->type              <br>";
    print " <li> Msg : $exc->message           <br>";
    print " <li> Docs: $exc->documentation_url <br>";
    print "<hr>";
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Complete example on how to parse an authorization code.

# Refer to our getting started guide for a complete API walkthrough
# https://labs.aweber.com/snippets/gs/using_the_api

from aweber_api import AWeberAPI, APIException

# set authorization_code to the code that is given to you from
# https://auth.aweber.com/1.0/oauth/authorize_app/YOUR_APP_ID
authorization_code = "Az12...|234k...|Aq53...|ka4l...|aj3ksf|"

try:
    auth = AWeberAPI.parse_authorization_code(authorization_code)
    consumer_key, consumer_secret, access_key, access_secret = auth

except APIException, exc:
    (exc_type, exc_msg) = str(exc).split(': ', 1)
    print '<h3>APIException:</h3>'
    print ' <li> Type: ' + exc_type
    print ' <li> Msg: ' + exc_msg
    print ' <li> Docs: ' + str(exc)
    print '<hr>'