Linkedin shows 403 forbidden error in java script api - javascript

I am trying to search people with company name but it displays in console as
GET https://api.linkedin.com/v1/people-search:(num-results,people:(first-name,last-name,distance))?company-name=infosys 403 (Forbidden) xdrpc.html?v=0.0.2000-RC8.35784-1413:1651
My Code :
<html>
<head>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: --api-key-here--
onLoad: onLinkedInLoad
authorize: true
</script>
<script type="text/javascript">
function onLinkedInLoad() {
alert("authenticating..");
// Listen for an auth event to occur
IN.Event.on(IN, "auth", onLinkedInAuth);
}
function onLinkedInAuth() {
IN.API.PeopleSearch()
.fields("firstName", "lastName", "distance")
.params({"company-name":"infosys"})
.result(displayPeopleSearch)
.error();
}
function displayPeopleSearch(peopleSearch){
var peopleSearchDiv = document.getElementById("peoplesearch");
var members = peopleSearch.people.values;
for (var member in members) {
// but inside the loop, everything is the same
// extract the title from the members first position
peopleSearchDiv.innerHTML += "<p>" + members[member].firstName + " " + members[member].lastName + " is a " + members[member].positions.values[0].title + ".</p>";
}
}
</script>
</head>
<!-- need to be logged in to use Search; if not, offer a login button -->
<script type="IN/Login"></script>
<body>
</body>
</html>

Found after a big battle,Linked IN has created vetted API access to people search only developers registered to that process can Access people search function

Related

Extract email address from Google account

I'm working on a website where the client has asked for an option to allow signup/login using Google and Facebook accounts. How can I extract the email address from a user's Google profile for storing in database?
Here is my code. The problem is that I am not getting the user profile completely. Instead, I am receiving just user name.
try
{
WebClient client = new WebClient();
var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="
+ access_token;
string outputData = client.DownloadString(urlProfile);
GoogleUserOutputData serStatus =
JsonConvert.DeserializeObject<GoogleUserOutputData>(outputData);
if (serStatus != null)
{
return serStatus;
// You will get the user information here.
}
}
catch (Exception ex)
{
//catching the exception
}
return null;
Here is a way to receive data (email, etc.) in JavaScript. At the end it shows an alert with data. (You can store this data in a database.) It's a complete working example with a Google button.
<html>
<head>
<title>Demo: Getting an email address using the Google+ Sign-in button</title>
<!-- Include the API client and Google+ client. -->
<script src = "https://plus.google.com/js/client:platform.js" async defer></script>
</head>
<body>
<!-- Container with the Sign-In button. -->
<div id="gConnect" class="button">
<button class="g-signin"
data-scope="email"
data-clientid="Your_Client_ID"
data-callback="onSignInCallback"
data-theme="dark"
data-cookiepolicy="single_host_origin">
</button>
<!-- Textarea for outputting data -->
<div id="response" class="hide">
<textarea id="responseContainer" style="width:100%; height:150px"></textarea>
</div>
</div>
</body>
<script>
/**
* Handler for the signin callback triggered after the user selects an account.
*/
function onSignInCallback(resp) {
gapi.client.load('plus', 'v1', apiClientLoaded);
}
/**
* Sets up an API call after the Google API client loads.
*/
function apiClientLoaded() {
gapi.client.plus.people.get({userId: 'me'}).execute(handleEmailResponse);
}
/**
* Response callback for when the API client receives a response.
*
* #param resp The API response object with the user email and profile information.
*/
function handleEmailResponse(resp) {
var primaryEmail;
var name;
var gender;
for (var i=0; i < resp.emails.length; i++) {
if (resp.emails[i].type === 'account')
primaryEmail = resp.emails[i].value;
if (resp.displayName != null)
name = resp.displayName;
gender = resp.gender;
}
document.getElementById('responseContainer').value = 'Primary email: ' +
primaryEmail + '\n\nFull Response:\n' + JSON.stringify(resp);
ShowAlert("Email: "+primaryEmail +" "+"Name: "+ resp.displayName +" "+"Gender: "+gender);
}
</script>
</html>
For further information and detail you can (should) read this link:
Getting people and profile information
Documentation is the key; please check it completely.
https://developers.google.com/identity/sign-in/web/sign-in
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<div class="g-signin2" data-onsuccess="onSignIn"></div>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
}
Sign out
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>

Can't logout with google-signin

I am trying to learn how to use Google Signin for my website. I am following the guide I found on this page: https://developers.google.com/identity/sign-in/web/
So I created a new document and I copy/past the code they propose on the above page. I added my client ID in the tag, and tested the code.
Everything worked well, I could login. The login button changed and said Signed in instead of Sign in. In addition, in I look into my console I can see the data from the console.log() like id, email, name,...
I continued reading the guide and I found how to add a Sign out button on this page: https://developers.google.com/identity/sign-in/web/sign-in
I implemented this code, and here is the full code I have (without the client ID)
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
Sign out
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
};
</script>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
</body>
</html>
When I click on it, I get the 'User signed out.' in my console. But if I refresh the page, the google button still says that I am Signed in, and the profile information are coming back in my console - even if I manually clear the console. The Sign out button clearly doesn't work, and I have no idea why.
Anybody has some experience with that?
Thanks for the help!
In order to call signOut you need to call call authorize() to get an id_token that is used to mark a user as signed out.
See this issue on the google-api-javascript-client GitHub.
A code sample is posted there explaining that you need to call gapi.auth.authorize first with a callback function that calls gapi.auth.signOut().
In essence, something like this:
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES,
'immediate': false,
cookie_policy: 'single_host_origin',
response_type: 'token id_token'
},
function (authResult) { gapi.auth.signOut();}
);

how to call getBasicProfile() of google to google signin on only button click?

i have take google signin in my website :
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
//console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
//console.log('Image URL: ' + profile.getImageUrl());
//console.log('Name: ' + profile.getName());
//console.log('Email: ' + profile.getEmail());
var user_uname = profile.getName();
var user_email = profile.getEmail();
alert(user_uname);
}
</script>
and here is a button to login google:
<div class="g-signin2" data-onsuccess="onSignIn"></div>
i want to give user google signin but the problem is whenever page is load onSignIn() function is called automatically.
i want it only on button click. can anybody help me?
Best solution is to render sign-in button only when user is not signed in.
<html>
<head>
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
</head>
<body>
<script>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
var user_name = profile.getName();
alert(user_name);
}
function onLoad() {
gapi.load('auth2,signin2', function() {
var auth2 = gapi.auth2.init();
auth2.then(function() {
// Current values
var isSignedIn = auth2.isSignedIn.get();
var currentUser = auth2.currentUser.get();
if (!isSignedIn) {
// Rendering g-signin2 button.
gapi.signin2.render('google-signin-button', {
'onsuccess': 'onSignIn'
});
}
});
});
}
</script>
<div id="google-signin-button"></div>
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</body>
</html>
I done it by declaring global js variable as false
var isFirstGoogle = 0;
Then to check this variable
if(isFirstGoogle)
{
//wont enter here first time
}
isFirstGoogle = 1;
So next time when I click on button the above method will be called as now isFirstGoogle = 1;
Hope this help!! It's a temporary thing I know but it's working for me.

Asp.Net MVC 3 : Client IP addres Using Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get Client IP using just Javascript?
I am working on .net mvc 3 application. i just want to get the ip address of the client.
How can i get client ip using JavaScript. If any body knows please share .
You can't do that with javascript. You could use javascript to send an AJAX request to a controller action that will return the IP of the client reading it from Request.UserHostAddress:
public ActionResult GetIP()
{
return Json(new { ip = Request.UserHostAddress }, JsonRequestBehavior.AllowGet);
}
and then:
var url = '#Url.Action("GetIP", "SomeController")';
$.getJSON(url, function(result) {
alert(result.ip);
});
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
window.onload = function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
document.getElementsByTagName("head")[0].appendChild(script);
};
function DisplayIP(response) {
document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
}
</script>
</head>
<body>
<form>
<span id = "ipaddress"></span>
</form>
</body>
</html>
<!-- Require jQuery / Anyversion --><script type="text/javascript" language="Javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- Require EasyJQuery After JQuery --><script type="text/javascript" language="Javascript" src="http://api.easyjquery.com/easyjquery.js"></script>
<script type="text/javascript" language="Javascript">
// 1. Your Data Here
function my_callback(json) {
alert("IP :" + json.IP + " nCOUNTRY: " + json.COUNTRY);
}
function my_callback2(json) {
// more information at http://api.easyjquery.com/test/demo-ip.php
alert("IP :" + json.IP + " nCOUNTRY: " + json.COUNTRY + " City: " + json.cityName + " regionName: " + json.regionName);
}
// 2. Setup Callback Function
// EasyjQuery_Get_IP("my_callback"); // fastest version
EasyjQuery_Get_IP("my_callback2","full"); // full version
</script>​
a working Example

FusionTables private table with OAUTH2

Good pic by Tim Rosenberg that shows exactly how OAUTH2 work's:
I'm kind a lazy to even start looking on this 2 files and test
so I searched for easyest way to
1.get token
2.access with that token
with help of gwt-oauth2
put it into index.php head :
<script type="text/javascript" src="gwt-oauth2.js"></script>
and this in body
<script type="text/javascript">
(function() {
var GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth";
var GOOGLE_CLIENT_ID = "CLIENT_ID";
//var PLUS_ME_SCOPE = "https://www.googleapis.com/auth/plus.me";
//var FusionTable_SCOPE = "https://www.googleapis.com/auth/fusiontables";
var button = document.createElement("button");
button.innerText = "Authenticate with Google";
button.onclick = function() {
var req = {
'authUrl' : GOOGLE_AUTH_URL,
'clientId' : GOOGLE_CLIENT_ID,
'scopes': ['https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/fusiontables'
],
};
oauth2.login(req, function(token) {
alert('Got an OAuth token:\n'+ token +'\n'+ 'Token expires in '+ oauth2.expiresIn(req) +' ms\n');
}, function(error) {
alert("Error:\n" + error);
});
};
var dv = document.getElementById('admin-content');
dv.appendChild(button);
var clearTokens = document.createElement('button');
clearTokens.innerText = 'Clear all tokens'
clearTokens.onclick = oauth2.clearAllTokens;
dv.appendChild(clearTokens);
})();
</script>
OK,
Now you can see connection and redirection to oauthWindow.html in new window without errors. GET parameters now showing you access_token token_type expires_in. Check the access_token HERE
As you see access_token working great BUT
What you still don't get is first alert from that :
oauth2.login(req, function(token) {
alert('Got an OAuth token:\n' + token + '\n'
+ 'Token expires in ' + oauth2.expiresIn(req) + ' ms\n');
}, function(error) {
alert("Error:\n" + error);
});
Second alert works fine and when you try to Auth. again if oauthWindow.html still open it shows you an error alert(so it's working!)
Now let's add that little code to oauthWindow.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
if (window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin) {
window.opener.oauth2.__doLogin(location.hash);
} else {
document.body.innerText = "Your browser seems to be stopping this window from communicating with the main window.";
}
</script>
</head>
<body></body>
</html>
Perfect!
Now if you want to work with private tables all you need is to add an access_token to url.
Thanks for giving me the reason to answer myself!
Put this into oauthWindow.html file
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
if (window.opener && window.opener.oauth2 && window.opener.oauth2.__doLogin) {
window.opener.oauth2.__doLogin(location.hash);
} else {
document.body.innerText = "Your browser seems to be stopping this window from communicating with the main window.";
}
</script>
</head>
<body></body>
</html>

Categories