From f5b1b462b1bd72a5c13eace9ba0a503ea7cf1370 Mon Sep 17 00:00:00 2001 From: Chris Petersen Date: Tue, 28 Jun 2011 16:07:33 -0700 Subject: [PATCH] New method to re-authorize based on an existing auth token so we don't have to send our users through the subpar already-authorized login experience. Store the token and expiration in window.localstorage and try to reauthorize before you authorize normally. --- .../plugins/facebook/FacebookAuth.java | 34 +++++++++++++++++-- Android/Facebook/www/facebook.js | 4 +++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Android/Facebook/src/com/hipsnip/plugins/facebook/FacebookAuth.java b/Android/Facebook/src/com/hipsnip/plugins/facebook/FacebookAuth.java index 9ed0013a..33df6507 100755 --- a/Android/Facebook/src/com/hipsnip/plugins/facebook/FacebookAuth.java +++ b/Android/Facebook/src/com/hipsnip/plugins/facebook/FacebookAuth.java @@ -54,6 +54,8 @@ public PluginResult execute(String action, JSONArray args, String callbackId) { if (action.equals("authorize")) { this.authorize(first); // first arg is APP_ID + } else if (action.equals("reauthorize")) { + this.reauthorize(first, args); // first arg is APP_ID } else if (action.equals("request")){ this.getResponse(first); // first arg is path } else if (action.equals("getAccess")){ @@ -150,13 +152,41 @@ public void authorize(final String appid) { final FacebookAuth fba = this; Runnable runnable = new Runnable() { public void run() { - fba.mFb = new Facebook(appid); - fba.mFb.setPlugin(fba); + if (fba.mFb == null) { + fba.mFb = new Facebook(appid); + fba.mFb.setPlugin(fba); + } fba.mFb.authorize((Activity) fba.ctx, fba.permissions, new AuthorizeListener(fba)); }; }; this.ctx.runOnUiThread(runnable); + } + /** + * Validate an existing token and expiration, and use them for the Facebook + * session, rather than re-authenticating the app (which presents a rather + * user experience in mobile). + * + * @return true if ok, or print a stack trace + */ + public void reauthorize(final String appid, JSONArray args) { + Log.d("PhoneGapLog", "reauthorize"); + final FacebookAuth fba = this; + final String access_token = args.optString(1, null); + final Long expires = args.optLong(2, -1); + try { + if (fba.mFb == null) { + fba.mFb = new Facebook(appid); + fba.mFb.setPlugin(fba); + } + if (access_token != null && expires != -1) { + fba.mFb.setAccessToken(access_token); + fba.mFb.setAccessExpires(expires); + } + } catch (JSONException e) { + e.printStackTrace(); + } + this.success(new PluginResult(PluginResult.Status.OK, fba.mFb.isSessionValid()), this.callback); } class AuthorizeListener implements DialogListener { diff --git a/Android/Facebook/www/facebook.js b/Android/Facebook/www/facebook.js index 873265bd..f1906047 100755 --- a/Android/Facebook/www/facebook.js +++ b/Android/Facebook/www/facebook.js @@ -14,6 +14,10 @@ PhoneGap.exec(callback, null, "FacebookAuth", "authorize", [app_id]); }; + Facebook.prototype.reauthorize = function(app_id, token, expires, callback) { + PhoneGap.exec(callback, null, "FacebookAuth", "reauthorize", [app_id, token, expires]); + }; + Facebook.prototype.setPermissions = function(permissions, callback) { PhoneGap.exec(callback, null, "FacebookAuth", "setPermissions", permissions); };