Skip to content

Instantly share code, notes, and snippets.

@edorcutt
Created December 30, 2011 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edorcutt/1541588 to your computer and use it in GitHub Desktop.
Save edorcutt/1541588 to your computer and use it in GitHub Desktop.
LinkedIn Application Authorization Demo using Kynetx OAuthModule
ruleset a169x484 {
meta {
name "LinkedIn-Authorize-OAuthModule"
description <<
LinkedIn Application Authorization Demo using Kynetx OAuthModule
Docs: http://docs.kynetx.com/docs/OAuthModule
Demo: http://ktest.heroku.com/a169x484
>>
author "Ed Orcutt, LOBOSLLC"
logging on
key errorstack "REDACTED"
use module a16x104 alias es
with es_key = keys:errorstack()
key linkedin {
"consumer_key" : "REDACTED",
"consumer_secret" : "REDACTED"
}
}
global {
// ------------------------------------------------------------------------
// Check for authorization to access LinkedIn API by making a protected request
LinkedInAuthorized = function() {
authCheck = oauthmodule:get('linkedin', {
'url' : 'http://api.linkedin.com/v1/companies/1866550?format=json'
});
(authCheck{"status_code"} == 200)
}
}
// ------------------------------------------------------------------------
// If we're authorized get our profile, otherwise you need to sign in
rule authorize_ruleset {
select when pageview ".*"
or web authorize_linkedin
if (LinkedInAuthorized()) then {
noop();
}
fired {
raise explicit event linkedin_profile
} else {
raise explicit event authorize_linkedin_button
}
}
// ------------------------------------------------------------------------
// Click the Signin button to start the OAuth dance
rule authorize_linkedin_button {
select when explicit authorize_linkedin_button
pre {
auth_button = <<
<img class="linkedin-auth" src="http://www.lobosllc.com/demo/linkedinauth/linkedin-signin.png" alt="Linkedin Login" />
>>;
auth_url = oauthmodule:get_auth_url('linkedin', {
'request_token_url' : 'https://api.linkedin.com/uas/oauth/requestToken',
'authorization_url' : 'https://api.linkedin.com/uas/oauth/authorize',
'access_token_url' : 'https://api.linkedin.com/uas/oauth/accessToken',
'raise_callback_event' : 'tryme'
});
appRID = meta:rid();
}
{
append("body", auth_button);
emit <<
// --------------------------------------------
$K('img.linkedin-auth').click( function() {
// Open popup window for Linkedin Authorization
var twAuthWindow = window.open(auth_url, "LinkedinAuthorizeWindow", "toolbar=no, location=yes, directories=no, status=no, menubar=no, scrollbars=no, menubar=no, resizable=no, width=700, height=500");
// Check to see if popup window is closed every 2 seconds
var twCheckTimer = window.setInterval( function() {
if (twAuthWindow.closed) {
clearInterval(twCheckTimer);
var app = KOBJ.get_application(appRID);
app.raise_event("authorize_linkedin");
};
}, 2000);
});
>>;
}
}
// ------------------------------------------------------------------------
// Retrieve our basic LinkedIn profile
rule LinkedIn_Profile {
select when explicit linkedin_profile
pre {
myProfile = oauthmodule:get('linkedin', {
'url' : 'http://api.linkedin.com/v1/people/~?format=json'
});
// Remove newlines from JSON, broken!
cleanHash = myProfile{"content"}.replace(re/\n/g, "").decode();
firstName = cleanHash{"firstName"};
lastName = cleanHash{"lastName"};
headline = cleanHash{"headline"};
fullName = "#{firstName} #{lastName}";
}
{
notify(fullName, headline) with sticky = true;
}
}
// ------------------------------------------------------------------------
rule linkedin_oauth_callback {
select when oauth_callback tryme
{ emit << window.close(); >>; }
fired { last; }
}
// ------------------------------------------------------------------------
// Error Handling in KRL
// http://www.windley.com/archives/2011/05/error_handling_in_krl.shtml
rule Catch_Errors_Throw_At_ErrorStack {
select when system error or user error
pre {
genus = event:param("genus");
species = event:param("species");
}
es:send_error("(#{genus}:#{species}) " + event:param("msg"))
with rule_name = event:param("rule_name")
and rid = event:param("rid");
}
// ------------------------------------------------------------------------
// Beyond here there be dragons :)
// ------------------------------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment