In my website, I have provided a facility to login via linkedin. I have used javascript API for that.
I have written below code for login.
function onLinkedInAuth() {
var isLoggedIn = document.getElementById('<%=hdnIsLoggedin.ClientID %>').value;
// Check for the session on the site
if (isLoggedIn == false || isLoggedIn == 'false') {
IN.API.Profile("me")
.fields("id", "firstName", "lastName", "industry", "emailAddress")
.result(function (me) {
//debugger;
var id = me.values[0].id;
var fname = me.values[0].firstName;
var lname = me.values[0].lastName;
var industry = me.values[0].industry;
var email = me.values[0].emailAddress;
// AJAX call to register/login this user
jQuery.ajax({
type: "POST",
url: "UserRegisterAsSeeker.aspx/LinkedinLoginOrRegister",
data: "{id:'" + id + "',fname:'" + fname + "',lname:'" + lname + "',email:'" + email + "'}",
contentType: "application/json; Characterset=utf-8",
dataType: "json",
async: true,
success: function (data) {
closeLoginWindow();
__doPostBack('<%=lnkRefreshPage.UniqueID%>', '');
},
error: function (request, status, error) {
alert('Unable to process the request at this moment! Please try again later.');
},
complete: function () {
}
});
});
}
}
Above code works fine.
And for logout i have used below function provided by linkedin
function logout() {
IN.User.logout(function () {
window.location.href = '/logout.aspx';
});
}
Problem is in logout. When we login to the site and immediately logout, it works fine but when we leave the site ideal for some time it does not logout from linkedin. We have to click 3-4 times then it works.
I don't know why it's happening. Please help if you can.
Related
function gameLogin() {
var usernameToPass;
console.log('Fetching information from facebool.... ');
FB.api('/me', function (response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
//pass user name to Servlet
usernameToPass = response.name;
pass(usernameToPass);
});
}
function pass(name) {
$.ajax({
url: 'GameManagerServlet',
data: {
username: name
},
type: 'GET',
success: function (name) {
console.log(name);
}
});
}
so basically i have this script in my jsp using Facebook login api got username then stored in a variable, and trying to pass to my servlet by another function, because my servlet needs to receive the username when the page is loaded, i've tried some ways like by ajax, but the server side when i use request.getParameter("username"); but always got null. Could you help me to fix this problem? Thanks a lot!
You should use callback function like below.
var usernameToPass;
function gameLogin() {
console.log('Fetching information from facebool.... ');
FB.api('/me', function (response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
//pass user name to Servlet
usernameToPass = response.name;
pass(callback){
callback(usernameToPass);
};
}
function pass(function(name) {
$.ajax({
url: 'GameManagerServlet',
data: {
username: name
},
type: 'GET',
success: function (successname) {
console.log(successname);
}
});
})
I would like to get multiple tweets of some hashtag, for example "iPhone".
Use the following code, but this will result in an error that sends the URL in red.
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: "http://search.twitter.com/search.json?q=iPhone&count=100",
success: function(response) {
console.log(response);
},
error: function(data) {
console.log('We have a problem!');
}
});
Is your error
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
Please refer https://dev.twitter.com/rest/public/search:- Please note that now API v1.1 requires that the request must be authenticated, check Authentication & Authorization documentation for more details on how to do it.
use titter4j library and following code
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("your key")
.setOAuthConsumerSecret(
"your key")
.setOAuthAccessToken(
"your key")
.setOAuthAccessTokenSecret(
"your key");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query("#iphone7");
query.setCount(100);
try {
result = twitter.search(query);
tweets.addAll(result.getTweets());
System.out.println("Gathered " + tweets.size() + " tweets");
} catch (TwitterException te) {
System.out.println("Couldn't connect: " + te.toString());
}
I'm using Facebook login feature & transferring some variables to other page through ajax. This particular ajax code isn't working, data is not going through. However I've other ajax code in other pages that works pretty good.
I'm not able to find the defect in the code.
Here is the code:
Page where ajax is called
<script type='text/javascript'>
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXX',
status : false,
cookie : true,
xfbml : true
});
}
function Login()
{
FB.login(function(response) {
if (response.authResponse)
{
getUserInfo();
}
else
{
console.log('User cancelled login or did not fully authorize.');
}
},{scope:'email,public_profile'});
}
function getUserInfo() {
FB.api('/me/permissions', function(response) {
var permission_response = JSON.stringify(response);
var permissions = eval('('+permission_response+')');
$.ajax({
type: "POST",
url: "/ajax_save_facebook_data.php",
data: {permissions:permissions},
success: function(option){
alert(option); // Nothing coming here, blank alert
}
});
});
}
AJAX Page
$permissions=$_POST['permissions'];
echo $permissions;
Anybody can help in this? I will really appreciate it.
FB.api is asynchrone function, so your var permissions doesn't exist when you do your ajax call.
Try:
function getUserInfo() {
FB.api('/me/permissions', function(response) {
var permission_response = JSON.stringify(response);
var permissions = eval('('+permission_response+')');
$.ajax({
type: "POST",
url: "/ajax_save_facebook_data.php",
data: {permissions:permissions},
success: function(option){
alert(option); // Nothing coming here, blank alert
}
});
});
}
I have the below Login JavaScript for my MVC4 Application. I am attempting to add a value with my POST data to update the [LastVisitDate] each time a user logs in. However, whenever I login with my credentials, the value is still shown as 4/18/2014 10:04:47 AM. I have verified my cache has been cleared and am at a loss as to what I may be doing wrong.
Anyone have some thoughts on the matter?
function login()
{
var userName = $("#username").val();
var password = $("#password").val();
var rememberme = $("#rememberMe").is(':checked');
// NEWLY ADDED
var datestamp = Date.now();
var data =
{
Email: userName,
Password: password,
RememberMe: rememberme,
// NEWLY ADDED
LastVisitDate: datestamp
};
$.ajax(
{
url: "/Account/Login",
type: "POST",
data: data,
cache: false,
async: true,
success: function (data) {
$("#loginAlert").remove();
if (data.returnUrl != undefined) {
window.location.href(data.returnUrl);
}
else {
window.location.reload();
}
},
error: function (result) {
if ($("loginAlert").length() == 0) {
var errorMsg = 'There was an error.';
$("#navBar").after(errorMsg);
}
}
});
}
function EnterKeyPressed () {
if (event.keyCode == 13) {
login();
}
}
function closeAlert() {
$("#loginAlert").alert("close");
}
You have just posted the Ajax code and there is no clear information on what is going on in the background.
But based on your question I am assuming you are not returning the last logged in date. If its a SQL query then you need to perform a Order by date DESC. See if it helps :)
I'm having an issue with the javascript below in which the $.ajax call is not being called. The alert('foo') does happen, but then the data call is completely skipped and the callback is never reached (never get alert('success!'). I don't completely understand callbacks, but this seems like it should be working.
Edit
Edited the script to where I currently stand, as I've read this way is better practice. Still, I can step in to authenticate(), it breaks on url:[...], but then never actually makes the ajax call. I've tried removing the return just to see if that's the problem, but it's producing the same result.
define(["jQuery", "kendo", "modernizr", "app/environment"], function ($, kendo, modernizr, environment) {
var authenticate = function (username, password) {
return $.ajax({
url: environment.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
type: 'get',
dataType: 'jsonp'
});
}
var canLogin = function(data) {
alert('good');
}
return {
viewModel: kendo.observable({
username: null,
password: null,
authenticate: function () {
var username = this.get('username'),
password = this.get('password');
authenticate(username, password).done(canLogin);
}
})
}
});
Use a callback instead.
var canLogin = function (username, password, callback) {
$.ajax({
async: false,
url: config.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
type: 'GET',
dataType: 'jsonp',
error: function (x, t, r) {
alert('Error');
},
success: callback
});
}
// use
canLogin("user","passwd",function( data ){
alert("Im called on authentication success!");
});
The solution is a mixture of e.preventDefault() and using a callback in the ajax call. Just using the callback didn't resolve the issue, but adding preventDefault to the authenticate function and fixing the callback issue did.
The final, working version, now looks something like this:
define(["jQuery", "kendo", "modernizr", "app/environment"], function ($, kendo, modernizr, environment) {
function authenticate(username, password, callback) {
$.ajax({
url: environment.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
type: 'get',
dataType: 'jsonp',
success: callback,
error: function (x,t,r) {
console.log('error')
}
});
}
function login(canLogin, username, password) {
if (canLogin == false) {
alert('Incorrect username or password');
return;
}
alert('good');
}
return {
viewModel: kendo.observable({
username: null,
password: null,
authenticate: function (e) {
e.preventDefault();
var username = this.get('username'),
password = this.get('password');
authenticate(username, password, function (canLogin) {
login(canLogin, username, password);
});
}
})
}
});