Skip to content

Commit d55e843

Browse files
committed
Update Authorization and Timestamp usage
Followerwonk's Social Authority API was developed when it was a Moz product. It used an Authorization method called MozSigned. Since Followerwonk is no longer owned by, or associated with Moz, we've renamed MozSigned to WonkSigned to avoid the confusion. We still accept MozSigned, though, so existing code does not break. The Timestamp parameter was originally called Expires, which allowed the client to set the expiration policy. Long ago, while still a Moz product, that strategy changed. Expires was renamed Timestamp, meant to be the current time, allowing server-side request signature lifetime policy management. Just set Timestamp to the current time and if it is reasonably close to current time when received by the server, it will be honored. A shorter valid time window may be employed in the future. Examples previously set a time in the future, compatible with the old Expires policy. This update changes the examples to use current time for all Timestamps.
1 parent 590d11f commit d55e843

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

code-examples/perl.pl

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
die "Must supply --id and --key" unless $id && $key;
1717

18-
my $time = time + 500;
19-
my $signature = hmac_sha1_hex( "$id\n$time", $key );
20-
my $auth = "AccessID=$id;Timestamp=$time;Signature=$signature";
18+
my $now = time;
19+
my $signature = hmac_sha1_hex( "$id\n$now", $key );
20+
my $auth = "AccessID=$id;Timestamp=$now;Signature=$signature";
2121

2222
while ( my $names = join ',', splice @ARGV, 0, 99 ) {
2323
say http( GET "$uri?screen_name=$names",
24-
Authorization => "MozSigned $auth" )->as_json->response->dump;
24+
Authorization => "WonkSigned $auth" )->as_json->response->dump;
2525
}

code-examples/php.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
<?php
1+
<?php
22
$uri = 'https://api.followerwonk.com/social-authority';
3-
3+
44
// Enter your Access ID and Secret Key from http://followerwonk.com/social-authority below
55
$accessID = '';
66
$secretKey = '';
77

88

9-
$time = time() + 500;
10-
$signature = urlencode( base64_encode( hash_hmac( "sha1", "{$accessID}\n{$time}", $secretKey, true ) ) );
11-
$auth = "AccessID={$accessID};Timestamp={$time};Signature={$signature}";
9+
$now = time();
10+
$signature = urlencode( base64_encode( hash_hmac( "sha1", "{$accessID}\n{$now}", $secretKey, true ) ) );
11+
$auth = "AccessID={$accessID};Timestamp={$now};Signature={$signature}";
1212

1313
// Initialize an array of users. We call the API once for each of the usernames. There are more efficient alternatives.
1414
$users = array('ebinnion', 'perigrin', 'randfish');
1515

1616
foreach($users as $user){
1717
// Fetch the Json object and decode it into an array
1818
$response = json_decode( file_get_contents( "{$uri}?screen_name={$user};{$auth}" ), true );
19-
19+
2020
// Use the commented API call below to get the result returned as an object
2121
// $response = json_decode( file_get_contents( "{$uri}?screen_name=randfish;{$auth}" ));
22-
22+
2323
// Uncomment code block below to see entire Json response
2424
// echo '<pre>';
2525
// print_r($response);
@@ -34,4 +34,4 @@
3434
echo $user_id . '<br>';
3535
echo $social_authority . '<br>';
3636
}
37-
?>
37+
?>

code-examples/python.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class FollowerWonk(object):
3535
def social_authority(username):
3636
uri = 'https://api.followerwonk.com/social-authority'
3737

38-
datime = int(time() + 500)
38+
datime = int(time())
3939

4040
keyBin = follower_wonk_secret_key_str.encode('UTF-8')
4141
messageStr = "%s\n%s" % (follower_wonk_access_id_str, datime)

docs/Anatomy-of-a-Social-Authority-API-Call.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Anatomy of a Social Authority API Call
22

3-
Every request to the Social Authority API follows the same basic format:
3+
Every request to the Social Authority API follows the same basic format:
44

55
http://api.followerwonk.com/social-authority?{screen_name};{user_id};{AccessID};{Timestamp};{Signature}
66

77
Here's what each of these parts of this request do:
88

9-
* `http://api.followerwonk.com`
9+
* `http://api.followerwonk.com`
1010
Access the API by calling the hostname of the service `api.followerwonk.com` and the Resource you’re making the request to `/social-authority`.
1111

1212
* {screen_name} & {user_id}
@@ -21,9 +21,9 @@ These are query parameters that provide your credentials. For example:
2121

2222
To use signed authentication, append the following three query string parameters:
2323

24-
The AccessID parameter identifies the client in question. The value of this parameter must be your access ID, obtained when you generate yourAPI credentials.
25-
The Timestamp parameter is a Unix timestamp that indicates for how long this request is valid. This should be a time in the future, usually no more than several minutes later than the moment your client code initiates the transfer. Values that expire excessively far in the future will not be honored by the Mozscape API. Authentication timestamps must be in UTC in order to work properly.
26-
The Signature parameter is an HMAC-SHA1 hash of your Access ID (as it appears in the AccessID parameter), followed by a new line, followed by the Timestamp parameter, using your Secret Key. This hash must be base64 and URL-encoded before being placed in the request query string.
24+
The AccessID parameter identifies the client in question. The value of this parameter must be your access ID, obtained when you generate your API credentials.
25+
The Timestamp parameter is a Unix timestamp and indicates when the request was signed. This should be the current time. Values that are significantly from the current time will not be honored by the Social Authority API. Authentication timestamps must be in UTC in order to work properly.
26+
The Signature parameter is an HMAC-SHA1 hash of your Access ID (as it appears in the AccessID parameter), followed by a new line, followed by the Timestamp parameter, using your Secret Key. This hash must be Base64 and URL-encoded before being placed in the request query string.
2727
Once combined, a valid query string should look like the following:
2828

2929
`AccessID=member-MDczMjM1NGUtN2Y3Ny01OGI0LThkOGUtYzhlYWVlYjcxMTZk;Timestamp=1225138899;Signature=LmXYcPqc%2BkapNKzHzYz2BI4SXfC%3D`
@@ -32,11 +32,11 @@ For example, the example request above should compute the HMAC-SHA1 of the follo
3232

3333
`member-MDczMjM1NGUtN2Y3Ny01OGI0LThkOGUtYzhlYWVlYjcxMTZk <newline> 1225138899`
3434

35-
Once the HMAC-SHA1 of this string is created, the binary form must be base64 encoded. The result of the base64 encoding must be URL-encoded. This method of authentication is complicated, but you can find helpful examples in several languages in our Sample Code.
35+
Once the HMAC-SHA1 of this string is created, the binary form must be Base64 encoded. The result of the Base64 encoding must be URL-encoded. This method of authentication is complicated, but you can find helpful examples in several languages in our Sample Code.
3636

3737
## ? and ;
3838

39-
These little characters are important, so dont miss them. The ? separates the main URL from the query parameters, and the ; goes between multiple parameters. You’ll see the ; used in the example for authentication, which is just 3 parameters required by the service.
39+
These little characters are important, so don't miss them. The ? separates the main URL from the query parameters, and the ; goes between multiple parameters. You’ll see the ; used in the example for authentication, which is just 3 parameters required by the service.
4040
All of these elements together give you a valid request:
4141

4242
http://api.followerwonk.com/social-authority?screen_name=randfish;AccessID=member-MDczMjM1NGUtN2Y3Ny01OGI0LThkOGUtYzhlYWVlYjcxMTZk;Timestamp=1225138898;Signature=LmXYcPqc%2BkapNKzHzYz2BI4SXfC%3D

docs/code-examples/perl.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ Querying the Social Authority API is really quite simple. Here's an expanded exa
2323

2424
die "Must supply --id and --key" unless $id && $key;
2525

26-
my $time = time + 500;
27-
my $signature = hmac_sha1_hex("$id\n$time", $key);
28-
my $auth = "AccessID=$id;Timestamp=$time;Signature=$signature";
26+
my $now = time;
27+
my $signature = hmac_sha1_hex("$id\n$now", $key);
28+
my $auth = "AccessID=$id;Timestamp=$now;Signature=$signature";
2929

3030
while ( my $names = join ',', splice @ARGV, 0,99 ) {
31-
say http(
32-
GET "$uri?screen_name=$names",
33-
Authorization => "MozSigned $auth"
31+
say http(
32+
GET "$uri?screen_name=$names",
33+
Authorization => "WonkSigned $auth"
3434
)->as_json->response->dump;
3535
}
3636

@@ -48,30 +48,30 @@ We're stating that we'd like to use Perl 5.12.1. This version of Perl is the fir
4848
Next we bring in the external libraries we would like to use. There are three:
4949

5050
use HTTP::Thin::UserAgent;
51-
use Getopt::Long;
51+
use Getopt::Long;
5252
use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
53-
53+
5454

5555
[`HTTP::Thin::UserAgent`][1] is a small HTTP client that makes doing API style requests easier. [`Getopt::Long`][2] is a standard command line argument parser, and it ships with the core Perl distribution. Finally [`Digest::HMAC_SHA1`][3] is what we'll use to sign our requests.
5656

5757
Continuing on:
5858

5959
die "Must supply --id and --key" unless $id && $key;
60-
60+
6161
If we don't have the information we need to sign the requests we throw an exception telling the user that they need to supply the required arguments.
6262

6363
Next we set up our authentication credentials:
6464

65-
my $time = time + 500;
66-
my $signature = hmac_sha1_hex("$id\n$time", $key);
67-
my $auth = "AccessID=$id;Timestamp=$time;Signature=$signature";
65+
my $now = time;
66+
my $signature = hmac_sha1_hex("$id\n$now", $key);
67+
my $auth = "AccessID=$id;Timestamp=$now;Signature=$signature";
6868

6969
Then for batches of 100 names provided on the command line, we make the API request:
7070

7171
while ( my $names = join ',', splice @ARGV, 0,99 ) {
72-
say http(
73-
GET "$uri?screen_name=$names",
74-
Authorization => "MozSigned $auth"
72+
say http(
73+
GET "$uri?screen_name=$names",
74+
Authorization => "MozSigned $auth"
7575
)->as_json->response->dump;
7676
}
7777

0 commit comments

Comments
 (0)