How to get Page Access Token by code? - c#

I need to generate a Page Access Token for a webpage that I have, this token will be used by the webpage to post to its Facebook Page feed.
This is what I do :
Go to Graph API Explorer
Choose my app from the dropdown
Click Get Access Token
Choose correct permissions(manage_pages/user_events)
To get the Page Access Token I have to run me/accounts in the Graph API Explorer. This will show all the pages I got with a new Page Access Token (short lived(about 60 min)) to each one.
This Page Access Token will work just fine to use in my C# code to post to the feed.
Problem
When another user uses my webpage to post a post I need to generate a new Page Access Token(in code) and then use it to post the userpost to the site´s facebook feed. The problem is that I could clearly not be there to grant the post so how do I handle this?
I could obviously not run the me/accounts(in code) command to get the Page Access Token from here.
I have looked at extended Access Token but this will last 60 days and then it will be the same problem as above.

What you need is an Extended Page Token:
generate User Access Token with manage_pages and publish_actions
generate Extended User Token (valid for 60 days): https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=[app-id]&client_secret=[app-secret]&fb_exchange_token=[short-lived-token]
call /me/accounts with the Extended User Token to get Extended Page Tokens
Extended Page Tokens are valid forever, so you don´t need to generate a new one all the time.
Source (including information about all the Tokens): http://www.devils-heaven.com/facebook-access-tokens/

On https://developers.facebook.com/docs/pages/access-tokens
Getting Page Access Tokens
GET /{page-id}?fields=access_token&access_token={user_access_token}
The response will look like this:
{
"access_token": "{your-page-access-token}",
"id": "{page-id}"
}

Related

Generate pdf for given url at server side

I am looking to generate pdf document for the given url. The client will send ids and using that ids server will create a url and use pdf generator to produce pdf. The pdf is generated but for the login page and not the desired page.
Client is angular 10, server is .net core 3.1
The authentication cookies are assigned to the pdf coverter but still no luck.
code
private FileContentResult PdfGenerate(int bookId, int? pageId)
{
//generate pdf for this url. This url can be accessed in the browser
string url = $"{ConfigHelper.AngularServer}/portal/{bookId}/{pageId}";
}
Currently I have tried with SelectPdf and Synfusion but login page is converted to pdf and not the page in the url.
The problem is that when you access the page specified, you are automatically forwarded to the login page, because you are not authorized. What you need it so get authentication token (authenticate yourself before hand) and then send the actual request with token added to the cookies/headers (whatever system is used for login).
For human-reading of page via browser, browser does this action for you automatically. You go to login page, you authenticate (enter login/password, press confirm), server confirms you are authorized and provides you some token details. This token is then added to each of your request towards the webserver.
Additional info: There is example how it works with some code https://fullstackmark.com/post/21/user-authentication-and-identity-with-angular-aspnet-core-and-identityserver .
However there are many articles over the internet and this topic is too broad to have fully qualified answer. We need to know what authorization is used, what is the login address & what requirements are for the authorization, etc.

Not able to get user info ( who commented ) of comments in a facebook page using graph api explorer

I am trying to get all the comments and users who commented those.
Using graph API explorer, for a particular page using page access token, I am able to get all the comments for a post and users who commented.
But using facebook App, I am only able to get comments and not the users who commented.
The user has Admin role on that page.
following are the steps I am following :
This returns list of pages and there page access token.
https://graph.facebook.com/me/accounts?access_token=XXXXXXXXXXXX
This returns the list of posts in that page
https://graph.facebook.com/{page-id}/posts?access_token=XXXXXXXXXXXX
This returns the list of comments
https://graph.facebook.com/{post-id}/comments?access_token=XXXXXXXXXXXX
In the last step I am suppose to get user details along with comment.
What am I missing ?

How do I post to a Facebook Page using the Graph API without having my Facebook App reviewed?

The Facebook documentation has this page called Posting as a Page which I've been following.
I followed the instructions on how to obtain an "immortal" access token for the Page.
I've used the Access Token Debugger to check my token is correct. It has the right AppID, the right ProfileID (for the page), and the right UserID (for the owner of the page). It expires "Never", it is valid, and it has the right scopes: manage_pages, publish_pages, publish_actions, public_profile.
I then used the Facebook SDK for .NET to make the call to the API to post to the Page.
var facebookClient = new FacebookClient(accessToken);
facebookClient.Post(string.Format("{0}/feed", profileID), new
{
message = message
});
The post appears on the Page and if I hover "globe" icon to see it's visibility/accessibility it says "public".
So, it worked right? Well ... no.
It turns out only users assigned a role in the Facebook App can see that post, despite it looking like a normal post and saying it's public. For any other users or those who are not signed in it simply doesn't appear.
The documentation page Page Access Tokens and Permissions says:
Your app might not need to request these permissions because people posting are already set up with a role in your app's dashboard. If this is the case you do not need to submit your app for review. See the Roles tab in App Dashboard.
I notice it says "people" which is a touch ambiguous. Does that mean only Users can publish to a Page without being reviewed? You can't publish as the Page itself without the review?
I've tried adding the page to the "Developers" and "Testers" roles for the app using the Pages ProfileID, but it says it can't resolve that to a UserID or name. That makes sense, roles are for people rather than pages.
EDIT 1:
I've tried using the "Embed Post" function and that gave me a link to the post. I tried using the link with a user that can't see the post and got:
It's not expired, as the users that can see it can still see it. It's interesting that it says you might not be in the audience that can view it, but it's set to public which I believe means everyone should see it.
You need to make your app “live” (see top of Status&Review tab), otherwise content created through it will only be visible to people with a role in the app.
(This has nothing to do with permission review.)

Persist Facebook Access Token

I am running asp.net application on Facebook,which has multiple pages.
On landing page I obtain access token.
Question is how to persist that access token so that I can use it in other pages
to access current user data.
Current when I try following in other pages,
Facebook.FacebookAPI fbApi = new Facebook.FacebookAPI();
JSONObject currentUser = fbApi.Get("/me");
it breaks saying unauthorized.
You should just store the access token in your database along with a user ID reference. Alternatively you can just use a client side SDK like the Facebook Javascript SDK which will persist for you without having to go extra effort.
I don't think you're using the most recent version of the API. It looks like you're using the deprecated one from https://github.com/facebook/csharp-sdk#readme
If you're using the latest one, then you can persist the access token. Grab the latest from:
https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/#readme

Facebook Graph API "/userid/feed" returning Blank

I'm using Facebook C# SDK and I can't seem to get feed data back using Graph API.
I've obtained the following extended permissions from the user:
scope=offline_access,publish_stream,publish_checkins,create_event,read_stream,user_about_me,user_events,user_hometown,user_location,user_photos,read_friendlists,read_requests,user_checkins,user_relationships,user_online_presence,user_notes,user_likes,user_work_history
I have an access token for offline access.
I'm able to retrieve userid/friends information without a hitch, but can't seem to get feed data.
I get the following returned:
{
"data": [
]
}
I obtained the token with type="client_cred" if that makes a difference. The process runs with user offline, so I'm not using the "me" alias.
Can someone give some direction on what I'm doing wrong?
A token obtained with type=client_cred means "the application on behalf of itself", rather than "the application on behalf of a specific user". This means that it can only see stuff that's visible to all users. If you want to grab information on behalf of a particular user, you need to use that user's access token that you acquired. Since you asked for offline_access, when you acquire an access token for a user, that token won't expire quickly, and you can keep it around in a database.
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
Once you add this code. Facebook sets a cookie on you domain. The name of the cookie would be fbs_YourAppId .
You can read the cookie and get the access_token.
Alternately you can use a method provided by the Facebook Api Connect.js file to get the feed if the user is logged in.
FB.Api(). Please refer to http://developers.facebook.com/docs/reference/javascript/fb.api/ to know more about the method.
Happy coding :)

Categories