Titanium Appcelerator Instagram Code ... This code is almost two years old and I havent tested it in a while
/** | |
* | |
* this code was inspired by the work done by David Riccitelli | |
* | |
* Copyright 2011 Aaron K. Saunders, Clearly Innovative Inc | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a> | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
var InstagramMgr = {}; | |
(function() { | |
InstagramMgr.API_URL = "https://api.instagram.com/v1/"; | |
InstagramMgr.init = function(clientId, redirectUri, loginUser) { | |
InstagramMgr.clientId = clientId; | |
InstagramMgr.redirectUri = redirectUri; | |
InstagramMgr.ACCESS_TOKEN = null; | |
InstagramMgr.xhr = null; | |
}; | |
InstagramMgr.logout = function() { | |
showAuthorizeUI( | |
String.format('https://instagram.com/oauth2/authorize?response_type=token&client_id=%s&redirect_uri=%s', | |
InstagramMgr.clientId, | |
InstagramMgr.redirectUri) | |
); | |
return; | |
}; | |
/** | |
* displays the familiar web login dialog we all know and love | |
* | |
* @params authSuccess_callback method called when successful | |
* | |
*/ | |
InstagramMgr.login = function(authSuccess_callback) { | |
if (authSuccess_callback != undefined) { | |
InstagramMgr.success_callback = authSuccess_callback; | |
} | |
showAuthorizeUI( | |
String.format('https://instagram.com/oauth/authorize/?response_type=token&client_id=%s&redirect_uri=%s&scope=likes+comments&display=touch', | |
InstagramMgr.clientId, | |
InstagramMgr.redirectUri) | |
); | |
return; | |
}; | |
InstagramMgr.callMethod = function(method, params, success, error) { | |
// get the login information and let it roll!! | |
try { | |
if (InstagramMgr.xhr == null) { | |
InstagramMgr.xhr = Titanium.Network.createHTTPClient(); | |
InstagramMgr.xhr.autoEncodeUrl = false; | |
} | |
var url = null; | |
if ( InstagramMgr.ACCESS_TOKEN != null ) { | |
url = InstagramMgr.API_URL + method + "?access_token=" + InstagramMgr.ACCESS_TOKEN; | |
} else { | |
url = InstagramMgr.API_URL + method + "?client_id=" + InstagramMgr.clientId; | |
} | |
for ( var x = 0; x < params.length; x++ ) { | |
url = url + "&" + params[x][0] + "=" + params[x][1]; | |
} | |
Ti.API.debug(url); | |
InstagramMgr.xhr.open("GET", url); | |
InstagramMgr.xhr.onerror = function(e) { | |
Ti.API.error("InstagramMgr ERROR " + e.error); | |
Ti.API.error("InstagramMgr ERROR " + InstagramMgr.xhr.location); | |
if ( error ) { | |
error(e); | |
} | |
}; | |
InstagramMgr.xhr.onload = function(_xhr) { | |
Ti.API.debug("InstagramMgr response: " + InstagramMgr.xhr.responseText); | |
if ( success ) { | |
success(InstagramMgr.xhr); | |
} | |
}; | |
InstagramMgr.xhr.send(); | |
} catch(err) { | |
Titanium.UI.createAlertDialog({ | |
title: "Error", | |
message: String(err), | |
buttonNames: ['OK'] | |
}).show(); | |
} | |
}; | |
/** | |
* code to display the familiar web login dialog we all know and love | |
*/ | |
function showAuthorizeUI(pUrl) | |
{ | |
window = Ti.UI.createWindow({ | |
modal: (Ti.Platform.osname != 'android'), | |
fullscreen: (Ti.Platform.osname != 'android'), | |
width: '100%', | |
height: '100%' | |
}); | |
var transform = Ti.UI.create2DMatrix().scale(0); | |
view = Ti.UI.createView({ | |
top: 5, | |
width: '100%', | |
height: 450, | |
border: 10, | |
backgroundColor: 'white', | |
borderColor: '#aaa', | |
borderRadius: 20, | |
borderWidth: 5, | |
zIndex: -1 | |
//transform: transform | |
}); | |
closeLabel = Ti.UI.createLabel({ | |
textAlign: 'right', | |
font: { | |
fontWeight: 'bold', | |
fontSize: '12pt' | |
}, | |
text: '(X)', | |
top: 5, | |
right: 12, | |
width: 10, | |
height: 14 | |
}); | |
window.open(); | |
webView = Ti.UI.createWebView({ | |
top: 25, | |
width: '90%', | |
height: '90%', // new | |
url: pUrl, | |
autoDetect: [Ti.UI.AUTODETECT_NONE] | |
}); | |
Ti.API.debug('Setting:[' + Ti.UI.AUTODETECT_NONE + ']'); | |
webView.addEventListener('beforeload', | |
function(e) { | |
if (e.url.indexOf('clearlyinnovative') != -1 || e.url.indexOf('instagram') != -1) { | |
Titanium.API.debug('in before load ' + e); | |
authorizeUICallback(e); | |
webView.stopLoading = true; | |
} | |
}); | |
webView.addEventListener('load', authorizeUICallback); | |
view.add(webView); | |
closeLabel.addEventListener('click', destroyAuthorizeUI); | |
view.add(closeLabel); | |
window.add(view); | |
if (Ti.Platform.osname != 'android') { | |
var animation = Ti.UI.createAnimation(); | |
animation.transform = Ti.UI.create2DMatrix(); | |
animation.duration = 500; | |
view.animate(animation); | |
} else { | |
view.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.hide(); | |
} | |
catch(ex) | |
{ | |
Ti.API.debug('Cannot destroy the authorize UI. Ignoring.'); | |
} | |
}; | |
/** | |
* fires event when login fails | |
* <code>app:instagram_access_denied</code> | |
* | |
* fires event when login successful | |
* <code>app:instagram_token</code> | |
* | |
* executes callback if specified when creating object | |
*/ | |
function authorizeUICallback(e) | |
{ | |
Ti.API.debug('authorizeUILoaded ' + e.url); | |
Titanium.API.debug(e); | |
if (e.url.indexOf('#access_token') != -1) | |
{ | |
var token = e.url.split("=")[1]; | |
InstagramMgr.ACCESS_TOKEN = token; | |
Ti.App.fireEvent('app:instagram_token', { | |
data: token | |
}); | |
if (InstagramMgr.success_callback != undefined) { | |
InstagramMgr.success_callback({ | |
access_token: token | |
}); | |
} | |
destroyAuthorizeUI(); | |
} else if ('http://instagram.com/' == e.url) { | |
Ti.App.fireEvent('app:instagram_logout', {}); | |
destroyAuthorizeUI(); | |
} else if (e.url.indexOf('#error=access_denied') != -1) { | |
Ti.App.fireEvent('app:instagram_access_denied', {}); | |
destroyAuthorizeUI(); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment