Created
June 1, 2012 15:53
-
-
Save keith-miller/2853139 to your computer and use it in GitHub Desktop.
OAuth login example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% log in with the request token | |
%% will return the verifier | |
login(LoginUrl, User, Pass, RequestToken, Domain) -> | |
PostData = build_post_data([ | |
{"email", User}, | |
{"password", Pass}, | |
{"oauth_token", RequestToken} | |
]), | |
Request = {LoginUrl, [], "application/x-www-form-urlencoded", PostData}, | |
%% turn on cookies | |
httpc:set_options([{cookies, enabled}]), | |
%% make the log in request | |
case httpc:request(post, Request, [{autoredirect, true}, {relaxed, true}], []) of | |
{ok, {{_, 302, _}, LoginResponse, _}} -> | |
{_,Location} = lists:keyfind("location", 1, LoginResponse), | |
NewUrl = string:concat("https://", string:concat(Domain, Location)), | |
{ok, {{_, 200, _}, _, Result}} = | |
httpc:request(post, | |
{NewUrl, [], "application/x-www-form-urlencoded", PostData}, | |
[{autoredirect, true}], []); | |
{ok, {{_, 200, _}, _, Body}} -> | |
Result = Body; | |
Error -> | |
Result = Error, | |
erlang:error(Result) | |
end, | |
get_verifier(Result). | |
build_post_data(L) -> | |
build_post_data(L, ""). | |
build_post_data([], DataString) -> | |
string:substr(DataString, 1, string:len(DataString)-1); | |
build_post_data([{Key, Value}|T], DataString) -> | |
ThisString = string:concat(string:concat(string:concat(Key, "="), Value), "&"), | |
build_post_data(T, string:concat(DataString, ThisString)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment