psn_request.js
function Login(callback)
	{
		Log("Making OAuth Request...");
		// start OAuth request (use our internal GET function as this is different!)
		_URLGET(
			// URL
			urls.login_base + "/2.0/oauth/authorize",
			// query string
			GetOAuthData({
				response_type: "code",
				service_entity: "urn:service-entity:psn",
				returnAuthCode: true,
				state: "1156936032"
			}),
			// headers
			{
				'User-Agent': ParseStaches(useragent)
			},
			function(err, body, response)
			{
				if (err)
				{
					// return login error
					if (callback) callback(err);
					return;
				}
				else
				{
					// get the login path
					var login_referrer = response.req.path;

					Log("Logging in...");

					// now actually login using the previous URL as the referrer
					request(
						{
							url: urls.login_base + "/login.do",
							method: "POST",
				            headers:
				            {
				            	'User-Agent': ParseStaches(useragent),
				            	'X-Requested-With': headers["X-Requested-With"],
				                'Origin': 'https://auth.api.sonyentertainmentnetwork.com',
				                'Referer': login_referrer,
				            },
				            form:
				            {
				                'params': new Buffer(login_params).toString('base64'),
				                'j_username': login_details.email,
				                'j_password': login_details.password
				            }
			        	},
				        function (err, response, body)
				        {
				        	if (err)
				        	{
				        		// login failed
				        		Log("Failed to make login request");
				        		if (callback) callback(err);
				        		return;
				        	}
				        	else
				        	{
					        	Log("Following login...");
					        	
					            request.get(response.headers.location, function (err, response, body)
					            {
					                if (!err)
					                {
					                	// parse URL
					                	var result = url.parse(response.req.path, true);
					                	if (result.query.authentication_error)
					                	{
					                		// try to extract login error from website
					                		var error_message = /errorDivMessage\"\>\s*(.*)\<br \/\>/.exec(body);
					                		if (error_message && error_message[1])
					                		{
					                			Log("Login failed! Error from Sony: " + error_message[1]);
					                			if (callback) callback(error_message[1]);
					                		}
					                		else
					                		{
												Log("Login failed!");
								        		if (callback) callback("Login failed!");
								        	}

							        		return;
					                	}
					                	else
					                	{
					                		// no auth error!
					                		var auth_result = url.parse(result.query.targetUrl, true);
					                		if (auth_result.query.authCode)
					                		{
					                			Log("Got auth code: " + auth_result.query.authCode);
					                			auth_obj.auth_code = auth_result.query.authCode;
					                			if (callback) callback(false);
					                			return;
					                		}
					                		else
					                		{
					                			Log("Auth error " + auth_result.query.error_code + ": " + auth_result.query.error_description);
								        		if (callback) callback("Auth error " + auth_result.query.error_code + ": " + auth_result.query.error_description);
								        		return;
					                		}
					                	}
					                }
					                else
					                {
					                    Log("Auth code fetch error: " + err);
					                    if (callback) callback(err);
					                    return;
					                }
					            });
					        }
				        }
				    );
				}
			}
		);
	}
function Login(callback)
	{
		Log("Making OAuth Request...");
		// start OAuth request (use our internal GET function as this is different!)
		_URLGET(
			// URL
			urls.login_base + "/2.0/oauth/authorize",
			// query string
			GetOAuthData({
				response_type: "code",
				service_entity: "urn:service-entity:psn",
				returnAuthCode: true,
				state: "1156936032"
			}),
			// headers
			{
				'User-Agent': ParseStaches(useragent)
			},
			function(err, body, response)
			{
				if (err)
				{
					// return login error
					if (callback) callback(err);
					return;
				}
				else
				{
					// get the login path
					var login_referrer = response.req.path;

					Log("Logging in...");

					// now actually login using the previous URL as the referrer
					request(
						{
							url: urls.login_base + "/login.do",
							method: "POST",
				            headers:
				            {
				            	'User-Agent': ParseStaches(useragent),
				            	'X-Requested-With': headers["X-Requested-With"],
				                'Origin': 'https://auth.api.sonyentertainmentnetwork.com',
				                'Referer': login_referrer,
				            },
				            form:
				            {
				                'params': new Buffer(login_params).toString('base64'),
				                'j_username': login_details.email,
				                'j_password': login_details.password
				            }
			        	},
				        function (err, response, body)
				        {
				        	if (err)
				        	{
				        		// login failed
				        		Log("Failed to make login request");
				        		if (callback) callback(err);
				        		return;
				        	}
				        	else
				        	{
					        	Log("Following login...");
					        	
					            request.get(response.headers.location, function (err, response, body)
					            {
					                if (!err)
					                {
					                	// parse URL
					                	var result = url.parse(response.req.path, true);
					                	if (result.query.authentication_error)
					                	{
					                		// try to extract login error from website
					                		var error_message = /errorDivMessage\"\>\s*(.*)\<br \/\>/.exec(body);
					                		if (error_message && error_message[1])
					                		{
					                			Log("Login failed! Error from Sony: " + error_message[1]);
					                			if (callback) callback(error_message[1]);
					                		}
					                		else
					                		{
												Log("Login failed!");
								        		if (callback) callback("Login failed!");
								        	}

							        		return;
					                	}
					                	else
					                	{
					                		// no auth error!
					                		var auth_result = url.parse(result.query.targetUrl, true);
					                		if (auth_result.query.authCode)
					                		{
					                			Log("Got auth code: " + auth_result.query.authCode);
					                			auth_obj.auth_code = auth_result.query.authCode;
					                			if (callback) callback(false);
					                			return;
					                		}
					                		else
					                		{
					                			Log("Auth error " + auth_result.query.error_code + ": " + auth_result.query.error_description);
								        		if (callback) callback("Auth error " + auth_result.query.error_code + ": " + auth_result.query.error_description);
								        		return;
					                		}
					                	}
					                }
					                else
					                {
					                    Log("Auth code fetch error: " + err);
					                    if (callback) callback(err);
					                    return;
					                }
					            });
					        }
				        }
				    );
				}
			}
		);
	}
function Login ( callback ) { Log ( "Making OAuth Request..." ) ; // start OAuth request (use our internal GET function as this is different!) _URLGET ( // URL urls . login_base + "/2.0/oauth/authorize" , // query string GetOAuthData ( { response_type : "code" , service_entity : "urn:service-entity:psn" , returnAuthCode : true , state : "1156936032" } ) , // headers { 'User-Agent' : ParseStaches ( useragent ) } , function ( err , body , response ) { if ( err ) { // return login error if ( callback ) callback ( err ) ; return ; } else { // get the login path var login_referrer = response . req . path ; Log ( "Logging in..." ) ; // now actually login using the previous URL as the referrer request ( { url : urls . login_base + "/login.do" , method : "POST" , headers : { 'User-Agent' : ParseStaches ( useragent ) , 'X-Requested-With' : headers [ "X-Requested-With" ] , 'Origin' : 'https://auth.api.sonyentertainmentnetwork.com' , 'Referer' : login_referrer , } , form : { 'params' : new Buffer ( login_params ) . toString ( 'base64' ) , 'j_username' : login_details . email , 'j_password' : login_details . password } } , function ( err , response , body ) { if ( err ) { // login failed Log ( "Failed to make login request" ) ; if ( callback ) callback ( err ) ; return ; } else { Log ( "Following login..." ) ; request . get ( response . headers . location , function ( err , response , body ) { if ( ! err ) { // parse URL var result = url . parse ( response . req . path , true ) ; if ( result . query . authentication_error ) { // try to extract login error from website var error_message = / errorDivMessage\"\>\s*(.*)\<br \/\> / . exec ( body ) ; if ( error_message && error_message [ 1 ] ) { Log ( "Login failed! Error from Sony: " + error_message [ 1 ] ) ; if ( callback ) callback ( error_message [ 1 ] ) ; } else { Log ( "Login failed!" ) ; if ( callback ) callback ( "Login failed!" ) ; } return ; } else { // no auth error! var auth_result = url . parse ( result . query . targetUrl , true ) ; if ( auth_result . query . authCode ) { Log ( "Got auth code: " + auth_result . query . authCode ) ; auth_obj . auth_code = auth_result . query . authCode ; if ( callback ) callback ( false ) ; return ; } else { Log ( "Auth error " + auth_result . query . error_code + ": " + auth_result . query . error_description ) ; if ( callback ) callback ( "Auth error " + auth_result . query . error_code + ": " + auth_result . query . error_description ) ; return ; } } } else { Log ( "Auth code fetch error: " + err ) ; if ( callback ) callback ( err ) ; return ; } } ) ; } } ) ; } } ) ; }
Login to app and get auth token
Login to app and get auth token