Jaffer Wilson Jaffer Wilson - 1 year ago 103
Javascript Question

Facebook post extraction using Javascript sdk

I am trying to make an application that will help me in extracting the posts of the users, and even their email using the Facebook Login API. I tried the following but could not rectify where exactly from I can extract the text post and use it for my purpose of NLP experiment.

See the following code:

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<!-- Facebook Login Function -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.10&appId=APP-ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log(response.authResponse.accessToken);
}
FB.api('/me', function(response) {
console.log(JSON.stringify(response));
});
});
}
</script>

<div class="fb-login-button" data-max-rows="1" data-size="large" data-button-type="continue_with" data-show-faces="false" data-auto-logout-link="false" data-use-continue-as="true"></div>


I just want to pass the posts to a function where I can move further and make the NLP process work for it.

Here is the function that works as NLP process:

<script>

//Validation for Paste Text field
function validate()
{
console.log( "Inside validate");
//return false;
var obj1 = document.getElementById('for_analysis').value;
console.log("Pring value of text area")
console.log( obj1);

if(obj1.trim() == '')
{
console.log( "Empty space found");
alert("Please Enter some Text before submitting");
//obj1.focus();
return false;
}
else{
console.log( "No empty space found");
$(".trial").show();
//document.getElementById("chkjq").className = "chkjq";
return true;
}
}
//paste this code under head tag or in a seperate js file.
// Wait for window load
$(document).ready(function(){
console.log( "inside jquery");
$(window).on('load',function() {
// Animate loader off screen
console.log( "Page Loading");
//$(".se-pre-con").fadeOut("slow");;
//$(".trial").fadeOut("slow");;
//$("#download").remove();
});

$(".chkjq").click(function(){
//console.log( "Hello, world!");
//$(".trial").show();
});
$("#filebutton").click(function(){
//console.log( "Hello, world! file button");
$(".trial").show();
});
});
</script>


The above function is applied to a textarea from where I am getting the text and processing it. :

<form action ="/analyze/" method="POST" name ="sendtext" id="sendtext" onsubmit="javascript:return validate()" >
{% csrf_token %}
<textarea id ="for_analysis" name ="for_analysis" form="sendtext" style="background-color: lightgrey"></textarea>
<!-- <input id="for_analysis" type="textarea" name="for_analysis" > -->
<br/>
<button type="submit" class="box__button chkjq" id = "chkjq">UPLOAD</button>
<!--<button id="ana_text">Analyze</button> -->
<br/></form>


Now I want to perform the same by extracting text post from the FB. Kindly let me know what I can do to make it right?

Answer Source

Quick example - with FB.login instead of the login button:

const getPosts = () => {
    FB.api('/me/posts', (response) => {
        console.log(response);
    });
};

const getEmail = () => {
    FB.api('/me', {fields: 'name,email'}, (response) => {
        console.log(response);
    });
};

FB.login((response) => {
    if (response.authResponse) {
        //user just authorized your app
        getPosts();
        getEmail();
    }
}, {scope: 'email,user_posts'});

Additional info: http://www.devils-heaven.com/facebook-javascript-sdk-login/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download