Skip to content
Browse files

Add example client code.

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

0 comments on commit 8e09fc5

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