Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add new oauth_token_pair module.
  • Loading branch information
Tim Fletcher committed Aug 24, 2008
1 parent 224f3f9 commit a16ce5e154f5858f00f60e278957cdeba8981cbd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 48 deletions.
@@ -2,46 +2,28 @@

-export([get/2, get/3, get/4]).
-export([post/2, post/3, post/4]).
-export([tokens/1]).
-export([token/1]).
-export([token_secret/1]).


get(URL, Consumer) ->
get(URL, Consumer, [], []).
get(URL, Consumer, {[], []}, []).

get(URL, Consumer, {oauth_tokens, Tokens}) ->
get(URL, Consumer, Tokens, []);
get(URL, Consumer, Params) when is_list(Params)->
get(URL, Consumer, [], Params).
get(URL, Consumer, Params) when is_list(Params) ->
get(URL, Consumer, {[], []}, Params);
get(URL, Consumer, TokenPair) ->
get(URL, Consumer, TokenPair, []).

get(URL, Consumer, {oauth_tokens, Tokens}, Params) ->
get(URL, Consumer, Tokens, Params);
get(URL, Consumer, Tokens, Params) when is_list(Tokens) ->
http:request(oauth_request:url("GET", URL, Params, Consumer, Tokens)).
get(URL, Consumer, TokenPair, Params) ->
http:request(oauth_request:url("GET", URL, Params, Consumer, TokenPair)).

post(URL, Consumer) ->
post(URL, Consumer, [], []).
post(URL, Consumer, {[], []}, []).

post(URL, Consumer, {oauth_tokens, Tokens}) ->
post(URL, Consumer, Tokens, []);
post(URL, Consumer, Params) when is_list(Params) ->
post(URL, Consumer, [], Params).
post(URL, Consumer, {[], []}, Params);
post(URL, Consumer, TokenPair) ->
post(URL, Consumer, TokenPair, []).

post(URL, Consumer, {oauth_tokens, Tokens}, Params) ->
post(URL, Consumer, Tokens, Params);
post(URL, Consumer, Tokens, Params) when is_list(Tokens) ->
SignedParamsString = oauth_request:params_string("POST", URL, Params, Consumer, Tokens),
post(URL, Consumer, TokenPair, Params) ->
SignedParamsString = oauth_request:params_string("POST", URL, Params, Consumer, TokenPair),
Request = {URL, [], "application/x-www-form-urlencoded", SignedParamsString},
http:request(post, Request, [], []).

tokens({ok, {_,_,Data}}) ->
{ok, {oauth_tokens, oauth_params:from_string(Data)}};
tokens(Term) ->
Term.

token({oauth_tokens, Tokens}) ->
proplists:get_value(oauth_token, Tokens).

token_secret({oauth_tokens, Tokens}) ->
proplists:get_value(oauth_token_secret, Tokens).
@@ -5,26 +5,26 @@
-export([header/6]).


params_string(Method, URL, ExtraParams, Consumer, Tokens) ->
oauth_params:to_string(params(Method, URL, ExtraParams, Consumer, Tokens)).
params_string(Method, URL, ExtraParams, Consumer, TokenPair) ->
oauth_params:to_string(params(Method, URL, ExtraParams, Consumer, TokenPair)).

url(Method, URL, ExtraParams, Consumer, Tokens) ->
fmt:sprintf("%s?%s", [URL, oauth_params:to_string(params(Method, URL, ExtraParams, Consumer, Tokens))]).
url(Method, URL, ExtraParams, Consumer, TokenPair) ->
fmt:sprintf("%s?%s", [URL, oauth_params:to_string(params(Method, URL, ExtraParams, Consumer, TokenPair))]).

header(Realm, Method, URL, ExtraParams, Consumer, Tokens) ->
SignedParams = params(Method, URL, ExtraParams, Consumer, Tokens),
header(Realm, Method, URL, ExtraParams, Consumer, TokenPair) ->
SignedParams = params(Method, URL, ExtraParams, Consumer, TokenPair),
HeaderString = oauth_params:to_header_string(SignedParams),
fmt:sprintf("Authorization: OAuth realm=\"%s\", %s", [Realm, HeaderString]).

params(Method, URL, ExtraParams, Consumer, Tokens) ->
{Params, TokenSecret} = oauth_params(Tokens, Consumer, ExtraParams),
params(Method, URL, ExtraParams, Consumer, TokenPair) ->
{Params, TokenSecret} = oauth_params(TokenPair, Consumer, ExtraParams),
[{oauth_signature, oauth_signature:new(Method, URL, Params, Consumer, TokenSecret)}|Params].

oauth_params([], Consumer, ExtraParams) ->
{oauth_params(Consumer, ExtraParams), ""};
oauth_params(Tokens, Consumer, ExtraParams) ->
Params = [proplists:lookup(oauth_token, Tokens)|oauth_params(Consumer, ExtraParams)],
{Params, proplists:get_value(oauth_token_secret, Tokens)}.
oauth_params({[], TokenSecret}, Consumer, ExtraParams) ->
{oauth_params(Consumer, ExtraParams), TokenSecret};
oauth_params({Token, TokenSecret}, Consumer, ExtraParams) ->
Params = [{oauth_token, Token}|oauth_params(Consumer, ExtraParams)],
{Params, TokenSecret}.

oauth_params(Consumer, ExtraParams) ->
proplists_merge([
@@ -10,14 +10,14 @@ test() ->

test(Consumer) ->
RequestTokenURL = "http://term.ie/oauth/example/request_token.php",
test(Consumer, tee(oauth:tokens(oauth:get(RequestTokenURL, Consumer)))).
test(Consumer, tee(oauth_token_pair:new(oauth:get(RequestTokenURL, Consumer)))).

test(Consumer, {ok, RequestTokenPair}) ->
test(Consumer, RequestTokenPair) ->
AccessTokenURL = "http://term.ie/oauth/example/access_token.php",
AccessTokenResponse = tee(oauth:tokens(oauth:get(AccessTokenURL, Consumer, RequestTokenPair))),
AccessTokenResponse = tee(oauth_token_pair:new(oauth:get(AccessTokenURL, Consumer, RequestTokenPair))),
test(Consumer, AccessTokenResponse, [{bar, "baz"}, {method, "foo"}]).

test(Consumer, {ok, AccessTokenPair}, EchoParams) ->
test(Consumer, AccessTokenPair, EchoParams) ->
EchoURL = "http://term.ie/oauth/example/echo_api.php",
{ok, {_,_,Data}} = tee(oauth:get(EchoURL, Consumer, AccessTokenPair, EchoParams)),
tee(lists:keysort(1, oauth_params:from_string(Data))).
@@ -0,0 +1,19 @@
-module(oauth_token_pair).

-export([new/2]).
-export([new/1]).


new(Token, TokenSecret) ->
{Token, TokenSecret}.

new(_HttpResponse={ok, {_,_,Data}}) ->
new_from_params(oauth_params:from_string(Data));
new(HttpResponse) ->
HttpResponse.

new_from_params(List) ->
new(get(oauth_token, List), get(oauth_token_secret, List)).

get(Key, List) ->
proplists:get_value(Key, List).

0 comments on commit a16ce5e

Please sign in to comment.