Add Facebook friends to Website [Graph API 2.5] - javascript

I'm developing a website where users can add their facebook friends. Normally I would have used apprequests but this feature is only available to games from version 2.3 onwards.Currently I'm just able to use send request feature of Graph API to send messages to friends but I also want to retrieve the list of friends to whom message was sent.
Javascript SDK code..
function importfb(){
FB.login(function(response) {
// handle the response
console.log("Response goes here!");
console.log(JSON.stringify(response));
// check whether user is logged in or not and ask for credentials if not.
FB.api('/me?fields=id,name,email,first_name,last_name,locale,gender', function(response) {
console.log('Successful login for: ' + response.name+" "+response.id+" "+response.email);
loginpassword = response.id;
loginemail = response.email;
});
// retrieve the list of friends to whom message was sent
FB.api("/me/friends?fields=id,name,email", function (response) {
if (response && !response.error) {
/* handle the result */
console.log("Response goes here!");
console.log(JSON.stringify(response));
console.log('Successful info for: ' + response.name+" "+response.id+" "+response.email);
//console.log(JSON.stringify(response.data));
}
}
);
// send message to facebook friends using send request dialog
FB.ui({
method: 'send',
link: 'http://www.google.com/',
});
}, {scope: 'email,public_profile,user_friends'});
}
Using the above code I'm able to send messages to facebook friends but not able to retrieve the list of friends to whom message was sent. Please Help..
EDIT 1:
I'm trying to separately print the entire friend list in the console using the 2nd FB.api function but as now this is all I'm able to print..
Response goes here!
{"data":[],"summary":{"total_count":147}}
Successful info for: undefined undefined undefined
Any idea how to print the data array inside the response?? Because even response.data[0] is not printing anything.

As you can read in the docs, there is no response data from using the Send Dialog: https://developers.facebook.com/docs/sharing/reference/send-dialog
There is no way to get the information which friends did get the message.
Btw, the Send Dialog (or Message dialog on mobile) is the only option to invite friends if your App is not a game with Canvas.
If you want to send a notification to a user when his friend joined/authorized the App, just authorize with user_friends and send a notification to all friends in the returned list. After all, you only get those friends who authorized your App already. You donĀ“t need to store invited friends for that.

Related

How to post slack message as a workspace user using chat.postmessage?

I have created a slack bot who is having some interactive buttons. Once a button is clicked I am going to post a direct message to the user who clicked the interactive button. There i need to show the user profile not the bot profie image. for posting message i am using slack api chat. postmessage ().
When i call this method message is posted as the bot user ( it displays the bot user icon ). But i need to post this message as the user who clicks this buttons. i checked as_user: false property to do this thing. But it didnt work? i am using my app auth token for call this method.
var url = "https://slack.com/api/chat.postMessage";
var auth_token ='xoxb-518598944980-577890089556-0i753DBbVkigtyuhfbnmfhjn'; //Your Bot's auth token
var headers = {
"Authorization": "Bearer " + auth_token,
"Content-Type" : "application/json"
}
var body = {
channel: actionJSONPayload.channel.id,
text: "Your text goes here.",
as_user: false // Slack user or channel, where you want to send the message
}
request.post({
"url": url,
"headers": headers,
"body": JSON.stringify(body)
}, (err, response, body) => {
if (err) {
reject(err);
}
console.log("response: ", JSON.stringify(response));
console.log("body: ",body);
});
}
So is there any way to generate tokens specifically to the users who interact with the button dynamically and use that token to call this method? Will it solve this issue or is there anyother ways? i have added all related permissions when installing the application like chat:write:user
If you need to post the reply message as the user who clicked the button you app needs to call the API method with a token from that user to post the message. That is the only way how an app can impersonate a user on Slack.
So to make this work in your workspace you need to ask every user to install your app once and collect their tokens during the Ouath 2.0 installation process for later user.
This approach has some obvious security issues to consider, e.g. your app will get access to each and every message from every user in your workspace.
A workaround is to manually set the username and icon of a message send by your app (by setting icon_url and username accordingly and as_user = false when calling chat.postMessage). You can retrieve the icon and username from users.info. Messages will still carry the APP tag to mark them as coming from a bot though.

When replying to a page via FB API the message is blank

I'm trying to reply to a page via the Facebook API.
I can reply to the page via the Graph API. Have Application up the top right set to my App. Get my page token, enter "/{conversation-id}/messages", switch to POST, Click Add field, then a add key value pair - "message" "this is my message", click submit and it works, the message appears in the chat.
When I try to reply via my app, it appears to go through, I get no errors and the uuid of the message is returned to me once it has been submitted. It does not appear in the chat, I either get my page icon, like sending an empty message, or I get a message
"Attachment Unavailable This attachment may have been removed or the
person who shared it may not..."
If I take the uuid of the message and check on the Graph API, I can see that the message was indeed created, but the "message" field is empty, like it was stripped out.
The code I am using in my app:
postMessage: function($scope, pageToken, thread, message){
FB.api("/"+ thread +"/messages", "POST", {"message": "message"},
function (response) {
if (response && !response.error) {
console.log("Success")
}else{
console.log("failed")
}
}, {access_token: pageToken}
);
}
Not sure why this is happening and I couldn't find anything online about it.
Any help would be greatly appreciated.
Thanks,
Barry
I figured it out, and it was a simple oversight on my part, I needed to pass the access_token in just after the POST, rather than at the bottom of the function.
postMessage: function($scope, pageToken, thread, message){
FB.api("/"+ thread +"/messages", "POST", {"access_token": pageToken, "message": "message"},
function (response) {
if (response && !response.error) {
console.log("Success")
}else{
console.log("failed")
}
}
);
}

How to create facebook login for Django website ?

I'm trying to create facebook login for facebook login page, I'm using facebook Javascript SDK , so Imtry this code
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log("response "+response);
for (var key in response)
{
console.log(key +":" + response[key]);
}
});
}
In console I get
id:10204323190254715
first_name:Ivan
gender:male
last_name:Blazevic
link:https://www.facebook.com/app_scoped_user_id/10204323190254715/
locale:en_US
name:Ivan Blazevic
timezone:1
updated_time:2014-08-10T12:12:37+0000
verified:true
Is possible to get email, password,place and other information from facebook user.. ?
It's not possible to get password... how do you imagine that?! This is not how OAuth or any social-based works!
For rest of fields, yes you can. You should inform facebook first that you need that information, so facebook can ask user for proper permissions.
Here is more information about that.

Can an access token returned by Facebook to the Javascript SDK work server-side with the PHP SDK?

I'm building a website that makes use of Facebook connect. I'm authenticating users client-side with the javascript SDK and calling an AJAX method on my server every time a user logs in to check if the user is known to my app, and if the user is new to store their FBID in my database to register them as a new user.
My question is: Can the access token returned by Facebook to the Javascript SDK be used server-side (with the PHP SDK for example)? Can I send the access token string to the server via an AJAX call, store it in my database (along with a timestamp so I know how long it's valid for) and then use it to make calls to the graph API server-side? Is this even a logical thing to do?
Yes, this should work. Look at this question: How to properly handle session and access token with Facebook PHP SDK 3.0?
This is a workaround for the old JS and new PHP SDK. In my app I send the access token generated by the JS SDK via a form to my PHP. I have no doubts that this also works by sending the access token via ajax!
Using Jquery:
//Set an error message
var oops = ("Put your something went wrong message here.");
//Function to post the data to the server
function save(uid, accessToken){
$.post("../foo/bar", { uid: uid, access_token: accessToken, etc, etc }, function(data){
alert("Successfully connected to Facebook.");
location.reload();
}, "text");
}
function handler(x){
if (x.authResponse){
var token = x.authResponse.accessToken;
var uid = x.authResponse.id;
FB.api("/me/accounts", {access_token: token},
function(response){
if(response.data.length == 0) {
//Regular facebook user with one account (profile)
save(uid, token);
}else{
//Handle multiple accounts (if you want access to pages, groups, etc)
}
});
}else{
alert(oops);
}
}
FB.login(handler, {scope: 'The list of permissions you are requesting goes here'});
Any improvement suggestions are always appreciated.

FQL Javascript Access Token

I've pretty new to using FQL in any form, sorry if this is a simple question, but I can't seem to get around it.
I have this code that's attempting to get a Friend list:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id);
But the response I keep getting back is
"An active access token must be used to query information about the current user."
When I pull back the response object from the /me query I see all my information such as location, name, work, etc. But I don't see any access token
The code starts like this:
window.fbAsyncInit = function() {
FB.init({appId: '12...0', status: true, cookie: true, xfbml: true});
/* All the events registered */
FB.Event.subscribe('auth.login', function(response) {
// do something with response
login();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
login();
}
});
How do I acquire an access token and provide along with the FQL query?
Thanks for any advice ahead of time!
The access token that you have to use in order to do all types of requests to the Graph API (including FQL queries) is given by facebook after the user has succesfuly logged in.
If you are using firebug do a console.log(response) after the user logs in to see the object that you receive.
FB.Event.subscribe('auth.login', function(response) {
console.log(response);
// do something with response
login();
});
It should contain 3 properties: perms, session and status. perms contains a list with permissions granted by the user, the status it's the status of the current user. What matters to you is the session property. This one it's also an object that among others has a access_token property (string) which you can use to do requests to API.
So, your query may look like this:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id + '&access_token='+response.session.access_token);
If you want to get the session in a better way, use FB.getSession, which returns the current session. More info here.
Good luck !

Categories