Having added Twitter support to a bunch of my Lightroom plugins, I thought I'd go ahead and share the Lua routine that does the OAuth authentication.
The OAuth concept seems snazzy, but wow, the documentation is scattered and it was difficult to pin down exactly what was required. It would have been nice had they just shown a simple example all the way through, so perhaps this code can serve as that (at least for the subset of OAuth that Twitter requires).
This code depends on the SHA-1 and HMAC-SHA1 routines I released earlier, and unlike that code, is also dependent on the environment provided to plugins by Lightroom.
Here are some of the header docs, extracted from the file itself....
The code exposes two public functions:
Twitter_AuthenticateNewCredentials() Twitter_SendTweet(credential_bundle, status_text)
The first leads the user through the procedure to grant your application permission to send tweets on their behalf. It returns a “credential bundle” (a Lua table) that can be cached locally (such as in the plugin preferences — see LrPrefs) and used for sending subsequent tweets forever, or until the user your application at Twitter unpermissions.
For example, if you have TWITTER_CREDENTIALS in your exportPresetFields list (with its default set to nil) and P is the local copy of the property table for the plugin (e.g. as passed to sectionsForBottomOfDialog, you might have:
f:view {
bind_to_object = P,
place = 'overlapping',
fill_horizontal = 1,
f:static_text {
fill_horizontal = 1,
visible = LrBinding.keyIsNotNil 'TWITTER_CREDENTIALS',
LrView.bind {
key = 'TWITTER_CREDENTIALS',
transform = function(credentials)
return LOC("$$$/xxx=Authenticated to Twitter as @^1",
credentials.screen_name)
end
},
},
f:push_button {
visible = LrBinding.keyIsNil 'TWITTER_CREDENTIALS',
enabled = LrBinding.keyIsNotNil '_authenticating_at_twitter',
title = "Authenticate at Twitter",
action = function()
LrFunctionContext.postAsyncTaskWithContext("authenticate at twitter",
function(context)
context:addFailureHandler(function(status, error)
LrDialogs.message("INTERNAL ERROR", error, "critical")
end)
context:addCleanupHandler(function()
_authenticating_at_twitter = nil
end)
_authenticating_at_twitter = true
P.TWITTER_CREDENTIALS = Twitter_AuthenticateNewCredentials()
end)
end
}
}
and then later during export...
local P = exportContext.propertyTable
if P.TWITTER_CREDENTIALS then
local result = Twitter_SendTweet(P.TWITTER_CREDENTIALS,
"I just did something with Lightroom!")
if result == nil then
-- user has revoked permission, so we'll uncache the credential bundle
P.TWITTER_CREDENTIALS = nil
end
end
You will have to update the code to have it reference the specific Consumer Key and Consumer Secret granted to your application when you registered it at Twitter.
Thank you Jeffrey – you have just saved what little hair I have remaining!
I used this nice little plugin as start for my lua-oauth library. It helped me very well to get how all this works
.
You may find the lua-oauth plugin here: http://dracoblue.net/download/luaoauth-10/61/
- Jan