Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

facebook access token is invalid - trying to post to facebook website-"page" via app using a cron

I am trying to post to my website's Facebook fan-page via a cron on my server.

I created a user on Facebook named "John Doe", i made him admin on my website's Facebook fan-page and then i created an app from that user in sandbox mode, so only he, "John Doe" can "run" it.

I gave the necessary permissions to my app from "John Doe" who runs it.

I can manually get the access tokens through developers.facebook.com and use them to post from my php cron script to my website's Facebook fan-page through "John Doe"'s app.

The problem is, that in order for this to work, my "John Doe" facebook user must be logged-in.

What should i do so my website php cron script has always automatically valid access token so it can post to website's Facebook fan-page successfully via the application which the administrator "John Doe" is using and has allowed access?

Right now i manually log-in to facebook as "John Doe" and i am copy-pasting the access token from developers.facebook.com to my php script and it works.

(Succesfull posts in my website's Facebook fan-page are shown like "m-y-w-e-b-s-i-t-e shared a link via m-y-a-p-p" so i am pretty sure that apart from the access-tokens everything else is ok)

Thank you in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This question is very similar to my own question and I've spent 2 days trying to solve this. The solution is super simple.

  1. Set your app to Native/Desktop so you can get a 60-day token (you'll turn this to permanent later). Do this by going to https://developers.facebook.com/apps/ Edit settings > Advanced > Choose Native/Desktop in the App type.

  2. Go to https://developers.facebook.com/tools/explorer/ select your app, and Get Access Token

  3. In the Extended Permissions tab, select manage_pages and publish_stream

  4. Now you have a 60-day temporary token.

  5. To get your permanent token, go here and use your temporary token: https://graph.facebook.com/me/accounts?access_token=REPLACE_THIS_WITH_YOUR_TEMPORARY_TOKEN

You should see permanent access token to all Facebook pages which you are an admin


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...