Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add example client code.

  • Loading branch information...
commit 8e09fc55991acd95f48584f7a7817e67615120c4 1 parent 0a438af
Tim Fletcher authored
15 data/oauth_gcontacts_pkey.pem
... ... @@ -0,0 +1,15 @@
  1 +-----BEGIN RSA PRIVATE KEY-----
  2 +MIICXAIBAAKBgQC0YjCwIfYoprq/FQO6lb3asXrxLlJFuCvtinTF5p0GxvQGu5O3
  3 +gYytUvtC2JlYzypSRjVxwxrsuRcP3e641SdASwfrmzyvIgP08N4S0IFzEURkV1wp
  4 +/IpH7kH41EtbmUmrXSwfNZsnQRE5SYSOhh+LcK2wyQkdgcMv11l4KoBkcwIDAQAB
  5 +AoGAWFlbZXlM2r5G6z48tE+RTKLvB1/btgAtq8vLw/5e3KnnbcDD6fZO07m4DRaP
  6 +jRryrJdsp8qazmUdcY0O1oK4FQfpprknDjP+R1XHhbhkQ4WEwjmxPstZMUZaDWF5
  7 +8d3otc23mCzwh3YcUWFu09KnMpzZsK59OfyjtkS44EDWpbECQQDXgN0ODboKsuEA
  8 +VAhAtPUqspU9ivRa6yLai9kCnPb9GcztrsJZQm4NHcKVbmD2F2L4pDRx4Pmglhfl
  9 +V7G/a6T7AkEA1kfU0+DkXc6I/jXHJ6pDLA5s7dBHzWgDsBzplSdkVQbKT3MbeYje
  10 +ByOxzXhulOWLBQW/vxmW4HwU95KTRlj06QJASPoBYY3yb0cN/J94P/lHgJMDCNky
  11 +UEuJ/PoYndLrrN/8zow8kh91xwlJ6HJ9cTiQMmTgwaOOxPuu0eI1df4M2wJBAJJS
  12 +WrKUT1z/O+zbLDOZwGTFNPzvzRgmft4z4A1J6OlmyZ+XKpvDKloVtcRpCJoEZPn5
  13 +AwaroquID4k/PfI7rIECQHeWa6+kPADv9IrK/92mujujS0MSEiynDw5NjTnHAH0v
  14 +8TrXzs+LCWDN/gbOCKPfnWRkgwgOeC8NN3h0zUIIUtA=
  15 +-----END RSA PRIVATE KEY-----
26 src/oauth_client.erl
... ... @@ -0,0 +1,26 @@
  1 +-module(oauth_client).
  2 +
  3 +-compile(export_all).
  4 +
  5 +
  6 +get(URL, Params, Consumer, ResponseParams, Fun) ->
  7 + Token = oauth:token(ResponseParams),
  8 + TokenSecret = oauth:token_secret(ResponseParams),
  9 + get(URL, Params, Consumer, Token, TokenSecret, Fun).
  10 +
  11 +get(URL, Params, Consumer, Token, TokenSecret, Fun) ->
  12 + case tee(oauth:get(URL, Params, Consumer, Token, TokenSecret)) of
  13 + {ok, Response} ->
  14 + case oauth_http:response_code(Response) of
  15 + Code when Code >= 400 ->
  16 + {error, {http, Code}};
  17 + _ ->
  18 + Fun(Response)
  19 + end;
  20 + Response ->
  21 + io:format("Error requesting ~p~n~n~p~n~n", [URL, Response]),
  22 + {error, bad_response}
  23 + end.
  24 +
  25 +tee(Term) ->
  26 + io:format("~p~n", [Term]), Term.
49 src/oauth_gcal.erl
... ... @@ -0,0 +1,49 @@
  1 +%%
  2 +%% This is an example client for the Google Calendar API.
  3 +%%
  4 +%% In order to use this client you will need your own consumer
  5 +%% key/secret, which you can obtain by registering a domain with Google:
  6 +%%
  7 +%% http://code.google.com/apis/accounts/docs/OAuth.html
  8 +%%
  9 +%% https://www.google.com/accounts/ManageDomain
  10 +%%
  11 +%%
  12 +%% Make sure the 'crypto', 'inets', and 'ssl' applications are running.
  13 +%% Then call oauth_gcal:get_request_token/1 with your consumer tuple.
  14 +%% This should prompt you to authorize the request token. The consumer
  15 +%% tuple and the response parameters are stored in the process dictionary
  16 +%% for ease of use.
  17 +%%
  18 +%% Once you have granted access, call oauth_gcal:get_access_token/0,
  19 +%% and then oauth_gcal:get_feed/0. This should return {ok, XML}, where
  20 +%% XML is an xmerl xmlElement record. The oauth_google_client:titles/1
  21 +%% function can be used to extract all the entry titles from the data.
  22 +%%
  23 +
  24 +-module(oauth_gcal).
  25 +
  26 +-compile(export_all).
  27 +
  28 +-define(URL, "http://www.google.com/calendar/feeds/default/allcalendars/full").
  29 +
  30 +
  31 +get_request_token(Consumer) ->
  32 + oauth_google_client:get_request_token(Consumer, "http://www.google.com/calendar/feeds/").
  33 +
  34 +get_access_token() ->
  35 + oauth_google_client:get_access_token().
  36 +
  37 +get_feed() ->
  38 + AParams = oauth_google_client:access_token_response_params(),
  39 + oauth_client:get(?URL, [], oauth_google_client:consumer(), AParams, fun({_, Headers, _}) ->
  40 + % The first request to ?FEED_URL will return a redirect response.
  41 + % If we blindly follow the redirect the request will fail. This is
  42 + % because the new location contains an additional parameter ("gsessionid"),
  43 + % and so the OAuth signature will be invalid for this new request.
  44 + % Instead, the parameter is extracted and used to generate a fresh request.
  45 + Location = proplists:get_value("location", Headers),
  46 + {_, _, _, _, _, [$?|ParamsString]} = http_uri:parse(Location),
  47 + Gsessionid = proplists:lookup("gsessionid", oauth_uri:params_from_string(ParamsString)),
  48 + oauth_google_client:get_feed(?URL, [Gsessionid])
  49 + end).
29 src/oauth_gcontacts.erl
... ... @@ -0,0 +1,29 @@
  1 +%%
  2 +%% This is an example client for the Google Contacts Data API.
  3 +%%
  4 +%% Usage is similar to oauth_gcal. The 'crypto', 'inets', and 'ssl'
  5 +%% applications need to be running. Call get_request_token/0 first,
  6 +%% and then authorize the request token as prompted. You can then
  7 +%% call get_access_token/0 followed by get_feed/0.
  8 +%%
  9 +%% cf. http://groups.google.com/group/oauth/msg/0cf50121f946a889
  10 +%%
  11 +
  12 +-module(oauth_gcontacts).
  13 +
  14 +-compile(export_all).
  15 +
  16 +-define(URL, "http://www.google.com/m8/feeds/contacts/default/base").
  17 +
  18 +
  19 +consumer() ->
  20 + {"weitu.googlepages.com", "x", {rsa_sha1, "data/oauth_gcontacts_pkey.pem"}}.
  21 +
  22 +get_request_token() ->
  23 + oauth_google_client:get_request_token(consumer(), "http://www.google.com/m8/feeds").
  24 +
  25 +get_access_token() ->
  26 + oauth_google_client:get_access_token().
  27 +
  28 +get_feed() ->
  29 + oauth_google_client:get_feed(?URL).
54 src/oauth_google_client.erl
... ... @@ -0,0 +1,54 @@
  1 +-module(oauth_google_client).
  2 +
  3 +-compile(export_all).
  4 +
  5 +-include_lib("xmerl/include/xmerl.hrl").
  6 +
  7 +-define(REQUEST_TOKEN_URL, "https://www.google.com/accounts/OAuthGetRequestToken").
  8 +
  9 +-define(AUTHORIZE_TOKEN_URL, "https://www.google.com/accounts/OAuthAuthorizeToken").
  10 +
  11 +-define(ACCESS_TOKEN_URL, "https://www.google.com/accounts/OAuthGetAccessToken").
  12 +
  13 +
  14 +titles(Feed) ->
  15 + [Node#xmlText.value || Node <- xmerl_xpath:string("//feed/entry/title/text()", Feed)].
  16 +
  17 +get_request_token(Consumer) ->
  18 + get_request_token(Consumer, []).
  19 +
  20 +get_request_token(Consumer, Scope) ->
  21 + oauth_client:get(?REQUEST_TOKEN_URL, [{"scope", Scope}], Consumer, "", "", fun(Response) ->
  22 + RParams = oauth_http:response_params(Response),
  23 + URL = oauth:uri(?AUTHORIZE_TOKEN_URL, [{"oauth_token", oauth:token(RParams)}]),
  24 + put(oauth_google_consumer, Consumer),
  25 + put(oauth_google_request_token_response_params, RParams),
  26 + io:format("~nPlease authorize the token at the following address:~n~s~n~n", [URL]),
  27 + ok
  28 + end).
  29 +
  30 +get_access_token() ->
  31 + oauth_client:get(?ACCESS_TOKEN_URL, [], consumer(), request_token_response_params(), fun(Response) ->
  32 + Params = oauth_http:response_params(Response),
  33 + put(oauth_google_access_token_response_params, Params),
  34 + ok
  35 + end).
  36 +
  37 +consumer() ->
  38 + get(oauth_google_consumer).
  39 +
  40 +request_token_response_params() ->
  41 + get(oauth_google_request_token_response_params).
  42 +
  43 +access_token_response_params() ->
  44 + get(oauth_google_access_token_response_params).
  45 +
  46 +get_feed(URL) ->
  47 + get_feed(URL, []).
  48 +
  49 +get_feed(URL, Params) ->
  50 + oauth_client:get(URL, Params, consumer(), access_token_response_params(), fun(Response) ->
  51 + Body = oauth_http:response_body(Response),
  52 + {XML, _} = xmerl_scan:string(Body),
  53 + {ok, XML}
  54 + end).
52 src/oauth_termie.erl
... ... @@ -0,0 +1,52 @@
  1 +%%
  2 +%% This is an example client for the test server at http://term.ie/oauth/example/.
  3 +%%
  4 +%% The echo/0 function calls the server with the default params, using the
  5 +%% default (HMAC-SHA1) signature method. To call the server with different
  6 +%% params, use echo/1. To call the server with a different signature method,
  7 +%% use echo/2, together with consumer/1.
  8 +%%
  9 +
  10 +-module(oauth_termie).
  11 +
  12 +-compile(export_all).
  13 +
  14 +-export([echo/0, echo/1, consumer/1, echo/2]).
  15 +
  16 +
  17 +echo() ->
  18 + echo([{"bar", "baz"}, {"method", "foo"}]).
  19 +
  20 +echo(Params) ->
  21 + echo(Params, consumer(hmac_sha1)).
  22 +
  23 +consumer(SigMethod) ->
  24 + {"key", "secret", SigMethod}.
  25 +
  26 +echo(Params, Consumer) ->
  27 + oauth_client:get(url(oauth_termie_request_token_url), [], Consumer, "", "", fun(Response) ->
  28 + echo(Params, Consumer, oauth_http:response_params(Response))
  29 + end).
  30 +
  31 +echo(Params, Consumer, RParams) ->
  32 + oauth_client:get(url(oauth_termie_access_token_url), [], Consumer, RParams, fun(Response) ->
  33 + echo(Params, Consumer, RParams, oauth_http:response_params(Response))
  34 + end).
  35 +
  36 +echo(Params, Consumer, _, AParams) ->
  37 + oauth_client:get(url(oauth_termie_echo_url), Params, Consumer, AParams, fun(Response) ->
  38 + {ok, lists:keysort(1, oauth_http:response_params(Response))}
  39 + end).
  40 +
  41 +default(oauth_termie_request_token_url) ->
  42 + "http://term.ie/oauth/example/request_token.php";
  43 +default(oauth_termie_access_token_url) ->
  44 + "http://term.ie/oauth/example/access_token.php";
  45 +default(oauth_termie_echo_url) ->
  46 + "http://term.ie/oauth/example/echo_api.php".
  47 +
  48 +url(Name) ->
  49 + case get(Name) of
  50 + undefined -> default(Name);
  51 + Other -> Other
  52 + end.

0 comments on commit 8e09fc5

Please sign in to comment.
Something went wrong with that request. Please try again.