-
-
Save csemrm/28ea79a3f998e2f2881ac5dd27209ce5 to your computer and use it in GitHub Desktop.
Updated Instagram Integration with Appcelerator, all the files you need are here, be sure to include the promises library in your project
This file contains 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
exports.C = { | |
INSTAGRAM_CLIENT_ID : 'replace this with your public', //replace this with your public | |
INSTAGRAM_CLIENT_SECRET : 'replace this with your private key', //replace this with your private key | |
INSTAGRAM_CALLBACK_URL : 'replace this with your INSTAGRAM_CALLBACK_URL' | |
}; |
This file contains 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
var creds = require('credentials').C; | |
var instagramMgr = require('instagram-module'); | |
var client = new instagramMgr.Client(creds.INSTAGRAM_CLIENT_ID, | |
creds.INSTAGRAM_CLIENT_SECRET, | |
creds.INSTAGRAM_CALLBACK_URL); | |
client.login().then(function(_result) { | |
Ti.API.info('_result ' + JSON.stringify(_result)); | |
}, function error(_error) { | |
Ti.API.info('_error ' + JSON.stringify(_error)); | |
}); |
This file contains 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
//Public client interface | |
function Client(clientId, clientSecret, callbackUrl) { | |
this.INSTAGRAM_CLIENT_ID = clientId; | |
this.INSTAGRAM_CLIENT_SECRET = clientSecret; | |
this.INSTAGRAM_CALLBACK_URL = callbackUrl; | |
this.ENDPOINT = 'https://api.instagram.com/v1/'; | |
this.ACCESS_TOKEN = null; | |
this.LOGIN_PROMISE = null; | |
Ti.API.debug(JSON.stringify(this)); | |
}; | |
exports.Client = Client; | |
var window, webView, success_callback; | |
// REMEMBER TO INCLUDE THIS LIBRARY IN PROJECT https://github.com/kriskowal/q | |
var Q = require('q'); | |
/** | |
* @param authSuccess_callback | |
* @param _scope | |
*/ | |
Client.prototype.login = function(authSuccess_callback, _scope) { | |
var deferred = Q.defer(); | |
var scope, that = this; | |
scope = _scope || "likes+comments"; | |
that.LOGIN_PROMISE = deferred; | |
if (Ti.App.Properties.getString("INSTAGRAM.ACCESS_TOKEN") === null) { | |
if (authSuccess_callback !== undefined) { | |
success_callback = authSuccess_callback; | |
} | |
showAuthorizeUI(String.format('https://instagram.com/oauth/authorize/?response_type=token&client_id=%s&redirect_uri=%s&scope=%s&display=touch', that.INSTAGRAM_CLIENT_ID, that.INSTAGRAM_CALLBACK_URL, scope), that); | |
} else { | |
Ti.API.info('login: using saved token'); | |
that.ACCESS_TOKEN = Ti.App.Properties.getString("INSTAGRAM.ACCESS_TOKEN"); | |
if (that.ACCESS_TOKEN) { | |
// call the callback since we were successful | |
var results = { | |
access_token : that.ACCESS_TOKEN | |
}; | |
authSuccess_callback && success_callback(result); | |
deferred.resolve(result); | |
Titanium.App.Properties.removeProperty("INSTAGRAM.ACCESS_TOKEN"); | |
} | |
} | |
return deferred.promise; | |
}; | |
/** | |
* code to display the familiar web login dialog we all know and love | |
* | |
* @param pUrl | |
* @param _that - the context of this instance of this module | |
*/ | |
function showAuthorizeUI(pUrl, _that) { | |
var that = _that; | |
window = Ti.UI.createWindow({ | |
top : 0, | |
//modal : true, | |
fullscreen : true | |
}); | |
webView = Ti.UI.createWebView({ | |
url : pUrl, | |
scalesPageToFit : true, | |
touchEnabled : true, | |
top : 0, | |
backgroundColor : '#FFF' | |
}); | |
Ti.API.debug('Setting:[' + Ti.UI.AUTODETECT_NONE + ']'); | |
webView.addEventListener('beforeload', function(e) { | |
if (e.url.indexOf(that.INSTAGRAM_CALLBACK_URL) != -1 || e.url.indexOf('instagram') != -1) { | |
Titanium.API.debug('in before load ' + JSON.stringify(e)); | |
authorizeUICallback(e, that.LOGIN_PROMISE); | |
webView.stopLoading = true; | |
} | |
}); | |
webView.addEventListener('load', function(e) { | |
authorizeUICallback(e, that.LOGIN_PROMISE); | |
}); | |
window.add(webView); | |
window.open(); | |
}; | |
/** | |
* unloads the UI used to have the user authorize the application | |
*/ | |
function destroyAuthorizeUI() { | |
Ti.API.debug('destroyAuthorizeUI'); | |
// if the window doesn't exist, exit | |
if (window == null) { | |
return; | |
} | |
// remove the UI | |
try { | |
Ti.API.debug('destroyAuthorizeUI:webView.removeEventListener'); | |
webView.removeEventListener('load', authorizeUICallback); | |
Ti.API.debug('destroyAuthorizeUI:window.close()'); | |
window.close(); | |
} catch(ex) { | |
Ti.API.debug('Cannot destroy the authorize UI. Ignoring.'); | |
} | |
}; | |
/** | |
* fires event when login fails | |
* <code>app:4square_access_denied</code> | |
* | |
* fires event when login successful | |
* <code>app:4square_token</code> | |
* | |
* executes callback if specified when creating object | |
*/ | |
function authorizeUICallback(e, _loginPromise) { | |
Ti.API.debug('authorizeUILoaded ' + e.url); | |
Titanium.API.debug("authorizeUICallback " + JSON.stringify(e)); | |
Titanium.API.debug("e.url " + e.url); | |
debugger; | |
if (e.url && e.url.indexOf('#access_token') != -1) { | |
var token = e.url.split("=")[1]; | |
this.ACCESS_TOKEN = token; | |
Ti.App.fireEvent('app:instagram_token', { | |
"data" : token | |
}); | |
if (success_callback !== undefined) { | |
success_callback({ | |
access_token : token | |
}); | |
} | |
_loginPromise.resolve({ | |
access_token : token | |
}); | |
Titanium.API.debug("Saved Access Token " + token); | |
destroyAuthorizeUI(); | |
} else if ('http://instagram.com/' == e.url) { | |
Ti.App.fireEvent('app:instagram_logout', {}); | |
destroyAuthorizeUI(); | |
_loginPromise.resolve({ | |
access_token : null, | |
logout : true | |
}); | |
} else if (e.url.indexOf('#error=access_denied') != -1) { | |
Ti.App.fireEvent('app:instagram_access_denied', {}); | |
destroyAuthorizeUI(); | |
_loginPromise.resolve({ | |
access_token : null, | |
access_denied : true | |
}); | |
} | |
}; | |
/* | |
* Get a Instagram object. Argument hash: | |
* | |
* className: string name of the Instagram class | |
* objectId: The object ID of the object you're getting, no id will return all | |
* success: function to call on success | |
* error: function to call on error | |
* | |
*/ | |
Client.prototype.get = function(args) { | |
Ti.API.debug("Client.prototype.get: started"); | |
// get the login information and let it roll!! | |
var url, that = this; | |
try { | |
if (that.xhr == null) { | |
that.xhr = Titanium.Network.createHTTPClient(); | |
that.xhr.autoEncodeUrl = false; | |
} | |
if (that.ACCESS_TOKEN != null) { | |
url = that.ENDPOINT + args.path + "?access_token=" + that.ACCESS_TOKEN; | |
} else { | |
url = that.ENDPOINT + args.path + "?client_id=" + that.CLIENT_ID; | |
} | |
for (var x = 0; x < args.params.length; x++) { | |
url = url + "&" + args.params[x][0] + "=" + args.params[x][1]; | |
} | |
Ti.API.debug("Client.prototype.get " + url); | |
that.xhr.open("GET", url); | |
that.xhr.onerror = function(e) { | |
Ti.API.error("InstagramMgr ERROR " + e.error); | |
Ti.API.error("InstagramMgr ERROR " + that.xhr.location); | |
if (args.error) { | |
args.error(e); | |
} | |
}; | |
that.xhr.onload = function(_xhr) { | |
Ti.API.debug("InstagramMgr response: " + that.xhr.responseText); | |
if (args.success) { | |
args.success(that.xhr.responseText); | |
} | |
}; | |
that.xhr.send(); | |
} catch(err) { | |
Titanium.UI.createAlertDialog({ | |
title : "Error", | |
message : String(err), | |
buttonNames : ['OK'] | |
}).show(); | |
} | |
}; | |
/* | |
* Get a Instagram object. Argument hash: | |
* | |
* className: string name of the Instagram class | |
* objectId: The object ID of the object you're getting, no id will return all | |
* success: function to call on success | |
* error: function to call on error | |
* | |
*/ | |
Client.prototype.post = function(args) { | |
Ti.API.debug("Client.prototype.post: started"); | |
// get the login information and let it roll!! | |
var url, that = this; | |
try { | |
if (that.xhr == null) { | |
that.xhr = Titanium.Network.createHTTPClient(); | |
that.xhr.autoEncodeUrl = false; | |
} | |
if (that.ACCESS_TOKEN != null) { | |
url = that.ENDPOINT + args.path + "?access_token=" + that.ACCESS_TOKEN; | |
} else { | |
url = that.ENDPOINT + args.path + "?client_id=" + that.CLIENT_ID; | |
} | |
Ti.API.debug("Client.prototype.post " + url); | |
that.xhr.open("POST", url); | |
that.xhr.onerror = function(e) { | |
Ti.API.error("InstagramMgr ERROR " + e.error); | |
Ti.API.error("InstagramMgr ERROR " + that.xhr.location); | |
if (args.error) { | |
args.error(e); | |
} | |
}; | |
that.xhr.onload = function(_xhr) { | |
Ti.API.debug("InstagramMgr response: " + that.xhr.responseText); | |
if (args.success) { | |
args.success(that.xhr.responseText); | |
} | |
}; | |
that.xhr.send(args.params); | |
} catch(err) { | |
Titanium.UI.createAlertDialog({ | |
title : "Error", | |
message : String(err), | |
buttonNames : ['OK'] | |
}).show(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment