Created
July 13, 2012 22:40
-
-
Save diorahman/3108016 to your computer and use it in GitHub Desktop.
Facebook Login
This file contains hidden or 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 page = require('webpage').create() | |
var system = require('system') | |
var clientId = '399722263402550' | |
var address = "https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri=https://www.facebook.com/connect/login_success.html&type=user_agent&display=touch&scope={1}" | |
address = address.split('{0}').join(clientId).split('{1}').join(system.args[3]) | |
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6'; | |
var accessTokenScript = function(){ | |
var obj = {} | |
function isLoginPage(){ | |
return (document.querySelector('button[name=login]') && document.querySelector('input[name=pass]') && document.querySelector('input[name=email]')) | |
} | |
function isGrantPage(){ | |
return (document.querySelector('button[name=cancel_clicked]') && document.querySelector('button[name=grant_clicked]') ) | |
} | |
function isErrorPage(){ | |
return (document.querySelector('div.abb.acr.aps')) | |
} | |
if(isLoginPage() && !isErrorPage()){ | |
document.querySelector('input[name=email]').value = '{0}' | |
document.querySelector('input[name=pass]').value = '{1}' | |
document.querySelector('button[name=login]').click() | |
obj.state = 'login' | |
}else if(isGrantPage()){ | |
document.querySelector('button[name=grant_clicked]').click() | |
var arr = document.querySelectorAll('button') | |
for(var i = 0; i < arr.length; i++){ | |
if(arr[i].value == 'Allow All'){ | |
arr[i].click() | |
} | |
} | |
obj.state = 'grant_access' | |
} | |
else{ | |
if(isErrorPage()){ | |
obj.state = 'error' | |
obj.msg = document.querySelector('div.abb.acr.aps').children[0].innerHTML.toString().split('<br>')[0].split('<b>').join('') | |
}else{ | |
obj.state = 'access_token' | |
var tokenInfo = window.location.toString().split('#')[1]; | |
var tokenInfoArr = tokenInfo.split('&'); | |
for(var l = 0; l < tokenInfoArr.length; l++){ | |
var kv=tokenInfoArr[l].split('='); | |
obj[kv[0]]=kv[1]; | |
} | |
} | |
} | |
return JSON.stringify(obj) | |
} | |
var script = accessTokenScript.toString() | |
script = script.split('{0}').join(system.args[1]).split('{1}').join(system.args[2]) | |
page.open(address, function(status){ | |
if(status == 'success'){ | |
var res = page.evaluate(script) | |
var obj = JSON.parse(res) | |
if(obj.state == 'error' || obj.state == 'access_token'){ | |
console.log(res) | |
phantom.exit() | |
} | |
} else { | |
console.log({state : 'error', msg : 'Network error'}) | |
phantom.exit() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment