Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic set-up for Twitter authentication.

  • Loading branch information...
commit 4d66ed5254917b71539ec92bbc8bcede004237be 1 parent 9d7e3bb
@akoprow akoprow authored
Showing with 99 additions and 2 deletions.
  1. +1 −1  Makefile
  2. +94 −0 src/twitter.opa
  3. +4 −1 src/webshell.opa
View
2  Makefile
@@ -1,7 +1,7 @@
NAME = webshell.exe
SRC = service.opa editor.opa config.opa login.opa webshell.opa \
- calc.opa facebook.opa search.opa dropbox.opa
+ calc.opa facebook.opa search.opa dropbox.opa twitter.opa
SRCS = $(SRC:%=src/%)
all: $(NAME)
View
94 src/twitter.opa
@@ -0,0 +1,94 @@
+// license: AGPL
+// (c) MLstate, 2011, 2012
+// author: Adam Koprowski
+
+import stdlib.apis.{twitter, oauth}
+
+database Twitter.configuration /twitter_config
+
+type Twitter.credentials = {no_credentials}
+ or {string request_secret, string request_token}
+ or {Twitter.credentials authenticated}
+
+module TwitterConnect
+{
+
+ server config =
+ _ = CommandLine.filter(
+ { init: void
+ , parsers: [{ CommandLine.default_parser with
+ names: ["--twitter-config"],
+ param_doc: "APP_KEY,APP_SECRET",
+ description: "Sets the application data for the associated Twitter application",
+ function on_param(state) {
+ parser {
+ case app_key=Rule.alphanum_string [,] app_secret=Rule.alphanum_string:
+ {
+ /twitter_config <- { consumer_key: app_key,
+ consumer_secret: app_secret
+ }
+ {no_params: state}
+ }
+ }
+ }
+ }]
+ , anonymous: []
+ , title: "Twitter configuration"
+ }
+ )
+ match (?/twitter_config) {
+ case {some: config}: config
+ default:
+ Log.error("webshell[config]", "Cannot read Twitter configuration (application key and/or secret key)
+Please re-run your application with: --twitter-config option")
+ System.exit(1)
+ }
+
+ private TW = Twitter(config)
+ private TWA = OAuth(TW.oauth_params({fast}))
+
+ private redirect = "http://{Config.host}/connect/twitter"
+
+ function login(raw_token) {
+ error("YYY")
+ }
+
+ private function authenticate() {
+ match (TWA.get_request_token(redirect)) {
+ case {~error}:
+ Service.respond_with(<>Twitter authorization failed</>)
+ case {success: token}:
+ auth_url = TWA.build_authorize_url(token.token)
+ auth_state = {request_secret: token.secret, request_token: token.token}
+ { response: {redirect: auth_url},
+ state_change: {new_state: auth_state}
+ }
+ }
+ }
+
+ private function tweet(state, content) {
+ match (state) {
+ case {authenticated: creds}:
+ Service.respond_with(<>Unimplemented...</>)
+ default:
+ authenticate()
+ }
+ }
+
+ Service.spec spec =
+ { initial_state: Twitter.credentials {no_credentials},
+ metadata: {
+ id: "twitter",
+ description: "Managing Twitter account",
+ cmds: [
+ { cmd: "tweet [content]", description: "Publishes a given tween" }
+ ],
+ },
+ function parse_cmd(state) {
+ parser {
+ case "tweet" Rule.ws content=(.*) : tweet(state, Text.to_string(content))
+ }
+ }
+ }
+
+}
View
5 src/webshell.opa
@@ -11,8 +11,9 @@ WB = WBootstrap
calc = Service.make(Calc)
search = Service.make(Search)
dropbox = Service.make(DropboxConnect)
+twitter = Service.make(TwitterConnect)
-shell = Shell.build([calc.handler, search.handler, dropbox.handler])
+shell = Shell.build([calc.handler, search.handler, dropbox.handler, twitter.handler])
function focus(set) {
Log.warning("focus", set);
@@ -112,6 +113,8 @@ function connect(connector, raw_data) {
dispatcher = parser {
case "/connect/facebook?" data=(.*) ->
connect(FacebookConnect.login, data)
+ case "/connect/twitter?" data=(.*) ->
+ connect(TwitterConnect.login, data)
case "/connect/dropbox?" data=(.*) ->
connect(DropboxConnect.login(dropbox.fun_executor), data)
case .* ->
Please sign in to comment.
Something went wrong with that request. Please try again.