How to choose user or page in facebook API call - node.js

I want to be able to post on a Facebook page using the graph API but I am unable to as it gives me this error.
{ message: '(#200) If posting to a group, requires app being
installed in the group, and \\n' +
' either publish_to_groups permission with user token, or both pages_read_engagement \\n' +
' and pages_manage_posts permission with page token; If posting to a page, \\n' +
' requires both pages_read_engagement and pages_manage_posts as an admin with \\n' +
' sufficient administrative permission',
type: 'OAuthException',
code: 200,
fbtrace_id: 'ABHLK9ZxMynsgBhcPI3Q0DL'
}
I am using the following API call for my request
const apiurl = `/${pagename}/feed`;
FB.api(apiurl, 'post', { message: req.body.fpost }, (response) => {
if (!response || response.error) {
console.log(!response ? 'error occurred' : response.error);
return;
}
res.render('facebook');
});
Can anyone help with what I am missing and how can I choose between use and page like we can choose in the Graph API Explorer as seen below. I have my page PrudentSays as the under User or Page. How can I choose the same with the API request? I have chosen all the proper permissions for posting as mentioned in the error.
Graph API Explorer Example

Related

Access denied to created spreadsheet google node API

I'm using the google node API to generate a spreadsheet. The 3rd party library I use is the official one (https://github.com/google/google-api-nodejs-client). Here is the code I use to generate the spreadsheet:
var sheets = google.sheets('v4');
sheets.spreadsheets.create({
auth: auth,
resource: {
properties:{
title: "Google export"
}
}
}, (err, response) => {
if (err) {
console.log('The API returned an error: ' + err);
return;
} else {
console.log("Added");
res.send(response);
}
});
It works fine and generate the sheet, when I display the response object, I get the link to the spreadsheet:
spreadsheetUrl: "https://docs.google.com/spreadsheets/d/1XS_QHxPUQcGqZGAzFHkHZV-RSKsv5IpJazcjVpLTYkk/edit"
But when I try to open it in the browser, I can't: You need permission.
The thing is if I click on the Request access button, I think it actually sends a request to the client_email from my credential (user#project-174309.iam.gserviceaccount.com), so it doesn't work.
If I open the spreadsheet programmatically it works...
Does anyone know how to open the generated spreadsheet in a browser?
Here is a solution: https://github.com/google/google-api-nodejs-client/issues/804#issuecomment-327931203
Need to use Google Drive API to grant permissions for this file

Azure Functions - Can it be used with Office Add-in's?

I'm sure there are clever people who can make anything run on Azure functions but does it make sense at all to use it for running Office Add-in's? I read that it's ideal for running small pieces of code and that's what my add-in currently running on Azure as an web app is.
You wouldn't use Azure Functions to build an add-in -- but you absolutely could use it in conjunction with a regular website, for some small server-side processing.
Concrete example: for an Add-in that a colleague and I were building, we needed to obtain a user's GitHub permissions to post Gists on the user's behalf. GitHub uses an "authorization code grant type" flow (see https://developer.github.com/v3/oauth/), so the flow would be as follows:
We pop a dialog (using the recently-introduce Dialog API in Add-ins) to direct the user to https://github.com/login/oauth/authorize, which shows a pretty login UI.
If the user signs in and consents, GitHub sends an authorization code back to us. The code does little good for us in client-side JavaScript, but if we pass it to an Azure Function, we can exchange it for an access token. This must be done in some server-side code (i.e., a web server, or Azure Functions, as a super-lightweight form of a web server) so that we can pass in a Client Secret for the exchange -- which quite naturally wouldn't be secret in sniffable-out client-side JavaScript. Hence putting that code on the server.
If you're curious to see what that code was like, here it is:
var request = require('request');
module.exports = function (context, data) {
context.log('code: ' + data.code);
if ('code' in data) {
request.post({
url: 'https://github.com/login/oauth/access_token',
json: {
client_id: '################',
client_secret: '################',
redirect_uri: '################',
code: data.code
}
}, function (err, httpResponse, body) {
if (err) {
context.log('error: ' + err);
context.res = {
body: {
status: 500,
error: err
}
}
}
else {
context.res = { body: body };
}
context.done();
});
}
else {
context.res = {
status: 400,
body: { error: 'Please pass the GitHub code in the input object' }
};
context.done();
}
}

Facebook API Nodejs

I'm trying post a message as admin of a facebook page, but my code keeps posting as my personal account to the "wall" of the page, not as the page itself.
When authorizing my application I ask for the right permissionscopes, and accept them. Although, when I check my permissions with /{USER-ID}/permissions, it says that I've denied the application to manage my pages. Any ideas why?
Here's my code for posting:
var parameters = {};
parameters.message = "Hello world, this is dog.";
parameters.access_token = req.session.access_token;
FB.api('{PAGE-ID}/feed', 'post', parameters, function (result) {
if (!result) {
return res.send(500, 'error');
} else if (result.error) {
if (result.error.type == 'OAuthException') {
result.redirectUri = FB.getLoginUrl({ scope: 'publish_actions,manage_pages,publish_pages', state: encodeURIComponent(JSON.stringify(parameters)) });
}
console.log(result);
return res.send(500, result);
}
res.send(result);
});
And yes, of course I use my page id instead of the placeholder in my code above.
*Update: It seems like the permissions aren't even asked for when authorizing the application. I added the email scope to the list and it appears when I re-authorize the application.
Could this have anything to do with my application not beeing approved?
"Submit for Login Review
Some of the permissions below have not been approved for use by Facebook.
Submit for review now or read more."

Facebook access token not accepted to Open Graph using Node.js facebook-node-sdk

Posting an Open Graph action using the Node.js facebook-node-sdk module gives me {"error": "An active access token must be used to query information about the current user."}.
var FB = require( 'fb' )
var path = '/me?' + namespace + ':' + action_type_name
var body = {
access_token: myAccessToken,
myAction : myCustomObjectURL
}
FB.api( path, 'post', body, function( res )
{
if( res.error ) {
console.log( 'the call to open graph failed: ' + res.error.message );
}
else {
console.log( 'success' )
}
})
If I print this access token and use it with cURL (curl https://graph.facebook.com/me?access_token=myAccessToken), it returns correctly, no error. Facebook also shows that the app has permission to post as this user. So what am I doing wrong here?
D'oh, stupid question. It turns out I needed a slash instead of a question mark in the URL path: path = '/me/' + ...
Actually this is a bug of the node facebook api.
When you use a '?' in your api-calls the accesstoken gets appended with another '?'
https://github.com/Thuzi/facebook-node-sdk/blob/master/fb.js#L268
This leads to a api-call like this one: https://graph.facebook.com/me/friends?limit=10?access_token=vSAEFfionhaWFwai...
You see multible '?' in the request and Facebook can not find the accesstoken anymore because of this.

Facebook example not working in Connect-auth/Express

I am following the example at facebook Connect-auth exemple. I don't understand how to get this example working.
In this snippet of code taken from the previous link:
// Method to handle a sign-in with a specified method type, and a url to go back to ...
app.get('/signin', function(req,res) {
req.authenticate([req.param('method')], function(error, authenticated) {
if(authenticated ) {
res.end("<html><h1>Hello Facebook user:" + JSON.stringify( req.getAuthDetails() ) + ".</h1></html>");
}
else {
res.end("<html><h1>Facebook authentication failed: " + error + " </h1></html>");
}
});
});
I do not understand what this does [req.param('method')] mean? It it hard to understand how connect-auth and facebook work together. I keep getting authentication failed.
The first argument to authenticate is an array of authentication strategies to try, in this example the req.param['method'] is set in the URL (var sign_in_link further down in the code) to "facebook" which matches the one and only authentication strategy initialized in the use.

Resources