User location is undefined - facebook-graph-api

I am getting all other user info but location will always be undefined.
My FB location is enabled, the pop up ask for this permission ( hometown, current city) , so I get the hometown/name/etc but no location.
FB.login(function(response) {
if (response.status === 'connected')
{
console.log("connected");
console.log('Access Token: ' + response.authResponse.accessToken);
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
testAPI();
}
else
{
console.log("not connected");
}
},{scope: 'email,user_birthday,user_location,user_hometown'});
function testAPI()
{
console.log('Welcome! Fetching your information.... ');
FB.api('/me', {fields: 'name,email,birthday,hometown,location'}, (response) => {
console.log('name: ' + response.name);
console.log('email: ' + response.email);
console.log('birth: ' + response.birthday);
console.log('home: ' + response.hometown.name);
console.log('location: ' + response.location.name);
});
}
Tried also location.name and location, non works.
There is something about including a token in some examples here, but nobody talks about it clearly in FB, or here, and its unclear to me if you need it or not and when .

Related

How to get session callback info? AWS - Cognito - JavaScript

I am trying to get the AccessKeyID and the SecretKey (ultimately to programatically input into a CRUD operation on dynamoDB with fine grained access control).
Anywho, there are many API calls to get the credentials like getCredentialsForIdentity(). However, these all require further params, which requires more api requests and more params and so on.
Ive found a way to log a Cognito user in, and then check if they are logged in on for a profile page, where it can display the email and username:
var data = {
UserPoolId : _config.cognito.userPoolId,
ClientId : _config.cognito.clientId
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
var cognitoUser = userPool.getCurrentUser();
window.onload = function(){
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if (err) {
alert(err);
return;
}
console.log('session validity: ' + session.isValid());
console.log(session.Credentials.AccessKeyId); //<--THIS DOESNT WORK
cognitoUser.getUserAttributes(function(err, result) {
if (err) {
console.log(err);
return;
}
console.log(result);
document.getElementById("email_value").innerHTML = result[2].getValue();
document.getElementById("username").innerHTML = cognitoUser.getUsername();
});
});
}
}
console.log(session.Credentials.AccessKeyId); seems to be an invalid request. Im assuming there's some good callback info in the session, like the AccessKeyID and SecretKey, which is what I'm looking for.
session.Credentials.AccessKeyId will not work since session's output is:
e {idToken: t, refreshToken: e, accessToken: t, clockDrift: 0}
whereas AWS.config.credentials's output is:
Thus your code will be:
var data = {
UserPoolId : _config.cognito.userPoolId,
ClientId : _config.cognito.clientId
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
var cognitoUser = userPool.getCurrentUser();
window.onload = function(){
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if (err) {
alert(err);
return;
}
console.log('session validity: ' + session.isValid());
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: _config.cognito.IdentityPoolId
});
AWS.config.credentials.get(function(err) {
console.log(AWS.config.credentials); // * see above what your response will be
console.log(AWS.config.credentials.accessKeyId);
console.log(AWS.config.credentials.secretAccessKey);
});
}
}

How to get data from an Facebook apprequest response?

I'm trying to figure out how I can get data from an apprequest response:
function invite(provider) {
FB.ui({
method: 'apprequests',
message: 'Check this thing out!',
title: 'Request Title'
}, function(response) {
if (response && !response.error) {
console.log('3 friend invited.');
console.log(response);
} else {
console.log('You have to invite 2 friends.');
console.log(response);
}
});
}
console.log(response); returns:
Object {
request: "667131760069164",
to: Array[1]
}
request: "667131760069164"
to: Array[1] 0: "1476807679266627"
length: 1
I'm wondering how I can get the information stored in to: to check how many were invited.
Thoughts, anyone?
Try this
obj = JSON.parse(response);
console.log(obj.to[0][0]);
console.log(obj.to[0].length);
You can access first value in 'to' array by obj.to[0][0] and obj.to[0].length
I managed to get the result I wanted with:
if (response.to.length > 2) {
console.log('Success! You have invited ' + response.to.length + ' friends.');
} else {
console.log('You need to invite at least 3 friends')
}
Thanks for the suggestions.

Express Passport unable to get facebook friends list

When attempting to use the Facebook api to get the friends list of a verified account it seems to work except that the friends list returned is empty.
facebook.js
var https = require('https');
exports.getFbData = function(accessToken, apiPath, callback) {
var options = {
host: 'graph.facebook.com',
port: 443,
path: apiPath + '?access_token=' + accessToken, //apiPath example: '/me/friends'
method: 'GET'
};
var buffer = ''; //this buffer will be populated with the chunks of the data received from facebook
var request = https.get(options, function(result){
result.setEncoding('utf8');
result.on('data', function(chunk){
buffer += chunk;
});
result.on('end', function(){
callback(buffer);
});
});
request.on('error', function(e){
console.log('error from facebook.getFbData: ' + e.message)
});
request.end();
}
app.js
app.get('/', function (req, res) {
if (req.session.myID != null && req.session.myName != null) {
User.findOne({sessionID: req.session.myID, username: req.session.myName}, function (err, doc) {
if (err) {throw err}
else if (doc != null) {
facebook.getFbData(doc.facebookToken, '/me/friends', function(data){
console.log(data);
res.render('index');
});
}
else {
//console.log("not logged in");
res.render('index');
}
});
}
else {
//console.log("not logged in");
res.render('index');
}
});
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET',
callbackURL: "http://localhost:3000/auth/facebook/callback",
profileFields: ['id', 'displayName']
},
function(accessToken, refreshToken, profile, done) {
User.findOne(..., function(err, user) {
if (err) { return done(err); }
user.facebookID = profile.id;
user.facebookToken = accessToken;
user.save();
return done(null, user);
});
}
));
app.get('/auth/facebook', passport.authenticate('facebook',
{scope: 'user_friends'})
);
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
}
);
The console.log in the facebook.getFbData callback prints:
{"data":[]}
This code actually works correctly. However it does not fetch the whole friends list, only the list of friends who also have the app.

Why is FB.Event.subscribe no longer detecting when I've logged out of facebook?

If I'm logged into facebook the testAPI() function fires as it's supposed to, but when I log out of facebook and refresh the page where my init code is, nothing happens. My console should log "not logged in", or "not authorized", but it doesn't. I've tried clearing the cache, but it doesn't work.
FB.Event.subscribe('auth.authResponseChange', function(response) {
//this works
testAPI();
} else if (response.status === 'not_authorized') {
console.log("not authorized");
} else {
console.log("not logged in");
}
});
var testAPI = function() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
};
auth.authResponseChange only fires when the status actually change, in your case it never change.
What you want is to use FB.getLoginStatus.

Post to facebook page as user

Am working on facebook graph API for posting on facebook page.I have a webpage where each users can login and share contents on a facebook page.Consider certain organizations,each organization has there own facebook page.From my website any user having facebook account can come and share their feedbacks about that organization and that feedback should be posted in the facebook page of the particular organization.
I need to implement this using facebook javascript api,but am getting an error
The user hasn't authorized the application to perform this action","type":"OAuthException","code":200
Here is my code:
FB.api('/page_id/feed', 'post',
{
message : "It's awesome ...",
name : 'Feedback',
to: '',
from: '',
description : 'Your description'
},
function(response) {
if (!response || response.error) {
//alert(JSON.stringify(response.error));
console.log(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
}
Please help
Thanks
Try this:
function login() {
FB.login(function (response) {
if (response.authResponse) {
// connected
postFeedBack();
} else {
// cancelled
}
}, { scope: 'publish_stream' });
}
function postFeedBack() {
FB.api('/page_id/feed', 'post', {
message: "My Feedback"
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}