I have worked with LinkedIn API and need guidance about logging out from Linkedin API once I click Logout in my application. I have tried the following code:
function closeSession(){
IN.User.logout();
}
Logout In LinkedIn
I have also tried:
$('logout-link').click(function() {
IN.Event.on(IN,'logout', function() {
window.location.href = [site-logout-url];
});
IN.User.logout();
});
I tried to destroy the session in a browser by calling session_destroy()
Finally, I tried the answer I got from Stack Overflow for this question. I didn't get any right solution. Can anyone tell me the solution?
Make sure that you have include
<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script>
on your home page, i.e. where you are logging out.
Seems your onclick is not being called, try doing:
function closeSession(){
IN.User.logout();
return true;
}
Logout In LinkedIn
OR
<a href="#" class="myButton" onclick="closeSession()" id="logout-link">
Logout In LinkedIn
</a>
And js
function closeSession() {
if ( typeof IN === 'object' && typeof IN.User === 'object' && IN.User.isAuthorized() ) {
//user logged in linkedin
//call linkedin logout with websites logout function as callback
alert("I m inside linkedin object check.."); //do you see this
IN.User.logout(logout);
}
else {
logout();
}
}
function logout() {
window.location.href = "URL_TO_YOUR_LOGOUT.PHP";
}
I think you are redirected before your javascript gets executed, so try this way ?
JavaScript Code :
function closeSession(){
IN.User.logout();
location.href="logout.php";
}
HTML Code :
<span style="cursor:pointer" onclick="closeSession()">Logout In LinkedIn</span>
To make it work
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: <?php echo sfConfig::get('app_linkedin_api_key'); ?>
authorize: true
</script>
<script>
IN.Event.on(IN, "logout", function() {onLinkedInLogout();});
function onLinkedInLogout(){
// User is logged out
window.location.href='<?php echo url_for("#homepage");?>'
}
</script>
<?php echo __("Logout");?>
<div class="signin"><script type="in/Login" data-onAuth="onLinkedInAuth"></script>
<script type="text/javascript">
function onLinkedInAuth() {
IN.API.Profile("me")
.fields("id")
.result( function(me) {
var id = me.values[0].id;
//Do stuff like redirect...
})
}
</script>
There seems to be 2 different issues here. You can't logout of linked in from your server side script unless you call their function. When you land on logout.php you should have your session_destroy(); on top of the page. Make sure there isn't any session_start() on logout.php
You need to load the LinkedIn JavaScript platform before trying to call IN.User.logout(), you should run the code only after the javascript library has loaded completely.
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: mykey
authorize: true
onLoad: onLoad
</script>
<script type="text/javascript">
function onLoad() {
try {
IN.User.logout();
} catch (err) {
console.log(err);
}
location.href="index.php";
}
</script>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: mykey
authorize: true
onLoad: onLoad
</script>
<script type="text/javascript">
function onLoad() {
try {
IN.User.logout();
} catch (err) {
console.log(err);
}
location.href="index.php";
}
</script>
use session_destroy() in the code.
Related
I need to get auth code in JSON not in url after confirm popup google window. I try do it like in this google docs https://developers.google.com/identity/sign-in/web/server-side-flow
but this is doesnt work! What i do wrong ? Here is my code .
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '0101010101-p5a3fgbkeso3loqg157001l4deajshot.apps.googleusercontent.com',
scope:'https://www.googleapis.com/auth/drive'
// Scopes to request in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
});
}
</script>
</head>
<body>
<button id="signinButton">Sign in with Google</button>
<script>
$('#signinButton').click(function() {
// signInCallback defined in step 6.
auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(function(resp) {
var auth_code = resp.code;
});
});
I'm trying to integrate the freichat module to my website, but i got this error
TypeError: $ is not a function
I tried to encapsulate it with an anonymous function passing jQuery as the parameter , like this:
(function($) {
$(document).ready(function() {
// my custom code
});
})(jQuery);
I also put jQuery instead of $ and got the below issue:
jQuery is undefined
Any idea regarding this?
Codes:
<script src="JS/jquery.js"></script>
<script language="javascript">
$(document).ready(function () {
$("#lawyerrr").select2();
$("#fromm").select2();
$("#too").select2();
});
</script>
<?php
$ses = null; // Return null if user is not logged in
if (!empty($_SESSION['user'])) {
$loggedUser = $_SESSION['user'];
}
if(isset($loggedUser['idd']))
{
if($loggedUser['idd'] != null) // Here null is guest
{
$ses=$loggedUser['idd']; //LOOK, now userid will be passed to FreiChat
}
}
//echo $ses;
if(!function_exists("freichatx_get_hash")){
function freichatx_get_hash($ses){
if(is_file("C:/wamp64/www/freichat/hardcode.php")){
require "C:/wamp64/www/freichat/hardcode.php";
$temp_id = $ses . $uid;
return md5($temp_id);
}
else
{
echo "<script>alert('module freichatx says: hardcode.php file not
found!');</script>";
}
return 0;
}
}
?>
<script type="text/javascript" language="javascipt" src="http://localhost/freichat/client/main.php?id=<?php echo $ses;?>&xhash=<?php echo freichatx_get_hash($ses); ?>"></script>
<link rel="stylesheet" href="http://localhost/freichat/client/jquery/freichat_themes/freichatcss.php" type="text/css">
Add JQuery library to your document:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I am trying to call this Javascript function. When I hit my button, there is nothing loading. I set an id for my button. With the onclick I load the function onLinkedInLoad. But is it not that function I should call to activate the script? Can anybody see what I am doing wrong here?
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<button id="btn">Try Me</button>
<script type="text/javascript">
document.getElementById("btn").onclick = onLinkedInLoad;
</script>
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key: myKey
authorize: true
onLoad: onLinkedInLoad
</script>
<script type="text/javascript">
// Setup an event listener to make an API call once auth is complete
function onLinkedInLoad() {
IN.Event.on(IN, "auth", shareContent);
}
// Handle the successful return from the API call
function onSuccess(data) {
console.log(data);
}
// Handle an error response from the API call
function onError(error) {
console.log(error);
}
// Use the API call wrapper to share content on LinkedIn
function shareContent() {
// Build the JSON payload containing the content to be shared
var payload = {
"comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
"visibility": {
"code": "anyone"
}
};
IN.API.Raw("/people/~/shares?format=json")
.method("POST")
.body(JSON.stringify(payload))
.result(onSuccess)
.error(onError);
}
</script>
</body>
</html>
Update:
I get this in my console:
18 Uncaught ReferenceError: onLinkedInLoad is not defined
www.linkedin.com/uas/js/userspace?v=0.0.1191-RC8.56309-1429&apiKey=myKey&authorize=true&onLoad=onLinkedInLoad&secure=1&:
22 Uncaught Error: You must specify a valid JavaScript API Domain as part of this key's configuration.
I putted my APIkey in also. I just replaced in the code here with "myKey". I also tried with .addEventListener('click', onLinkedInLoad), but still nothing happens
Try to load the function definition script before the call script. Or better, take all the scripts in another file and just load that on your HTML.
I've tried your code on js fiddle and it works by doing that.
I want to integrate Linkedin login using javascript.
I searched for that and get relevant results. But lot of search results says that below code:
<script type="in/Login">
</script>
is used to create sign-in button. But i want to use my own custom button and call a function on "onClick" event in my HTML.
Help in correct direction.
My code :
function linkedinLogin(){
console.log('linkedinLogin called');
var src="http://platform.linkedin.com/in.js"
api_key: 'XXXXXXXXXXXXXXXX'
authorize: true
onLoad: OnLinkedInFrameworkLoad
}
function OnLinkedInFrameworkLoad()
{
IN.Event.on(IN, "auth", OnLinkedInAuth);
}
function OnLinkedInAuth() {
IN.API.Profile("me").result(ShowProfileData);
}
function ShowProfileData(profiles)
{
var member = profiles.values[0];
console.log(member);
var id=member.id;
var firstName=member.firstName;
var lastName=member.lastName;
var photo=member.pictureUrl;
var headline=member.headline;
//use information captured above
var str="<b>id</b> : "+id+"<br>";
str +="<b>firstName: </b>"+firstName+"<br>";
str +="<b>lastName: </b>"+lastName+"<br>";
str +="<b>photo: </b>"+photo+"<br>";
str +="<b>headline: </b>"+headline+"<br>";
str +="<input type='button' value='Logout' onclick='logout();'/>";
document.getElementById("status").innerHTML = str;
}
And this is my HTML snippet:
<li>
<a href="javascript:void(0);" onClick="linkedinLogin()">
<img src="images/icon_linkedIn.png" />
<span>LinkedIn</span>
</a>
</li>
<html>
<head>
<title>LinkedIn JavaScript API Hello World</title>
<!-- 1. Include the LinkedIn JavaScript API and define a onLoad callback function -->
<script type="text/javascript" src="https://platform.linkedin.com/in.js">
api_key: xxx
onLoad: onLinkedInLoad
authorize: true
</script>
<script type="text/javascript">
// 2. Runs when the JavaScript framework is loaded
function onLinkedInLoad() {
IN.Event.on(IN, "auth", onLinkedInAuth);
}
// 2. Runs when the viewer has authenticated
function onLinkedInAuth() {
IN.API.Profile("me").fields("id","first-name", "last-name", "email-address").result(displayProfiles);
}
// 2. Runs when the Profile() API call returns successfully
function displayProfiles(profiles) {
member = profiles.values[0];
document.getElementById("profiles").innerHTML =
"<p>"+member.id+"<br> " + member.firstName + "<br> " + member.lastName + "<br>"+member.emailAddress+"</p>";
}
</script>
</head>
<body>
<!-- 3. Displays a button to let the viewer authenticate -->
<script type="in/Login"></script>
<!-- 4. Placeholder for the greeting -->
<div id="profiles"></div>
</body>
</html>
Can you try this ?
Facebook login using the Javascript FB.getLoginStatus works in one case and does not work in anther.
This works:
//view
<script type="text/javascript">
function f1() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
FB.api('/me', function(response) {
alert(response.name + " " + response.id);
name1 = response.name;
uid1 = response.id;
});
// var uid = response.authResponse.userID;
// var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}
</script>
<%= content_for :body_attributes, "onload='f1();'" %>
This does not work
// view
<script type="text/javascript">
function f1() {
// same definition as above
}
</script>
<script type="text/javascript"> {
f1()
}
</script>
I do not understand why the second code does not work. Thanks!
<script type="text/javascript">
{
f1()
}
</script>
Remove the braces and add a semicolon and you should be ok:
<script type="text/javascript">
f1();
</script>
This works:
//view
<script type="text/javascript">
function f1 {
FB.getLoginStatus(function(response) {
Hard to believe that this should work – because that’s not how you define a function in JavaScript. It’s missing the round brackets () after the function name.
[from comments] Doesn't FB.api have to be trigged by an event?
No, it doesn’t – it can be called from anywhere.
Just with methods that will (/might) create a popup window (FB.login, FB.ui) it is usually better to call them on user interaction such as a click, because otherwise modern browser’s popup blockers with default settings are likely to suppress the popup.