动态帖子发布到页面墙一步一步我做了什么

发布于 2025-01-07 00:14:01 字数 2452 浏览 0 评论 0原文

好的,我终于成功地通过 PHP 将我网站上的动态新闻帖子与我的页面墙作为页面(而不是用户)链接起来。 我仍然想知道我的解决方案是否正确,或者从长远来看是否有效。 话虽如此,这就是我所做的:

  • 创建一个配置文件来管理页面
  • 创建公司页面
  • 创建一个应用程序,其域指向我的网站域,网站指向我的网站网址
  • 禁用offline_access弃用以便能够发出offline_access令牌
  • 通过 http://graph.facebook.com/PAGE_NAME 找到我的 pageID
  • 前往 https://developers.facebook.com/tools/explorer/APP_ID
  • 将 pageID 粘贴到而不是userID 并单击“提交”
  • 然后我单击“获取访问令牌”并检查了“manage_pages”、“publish_stream”、“offline_access”

(当仅此功能不起作用时,我访问了 https://developers.facebook.com/docs/authentication/ 在“页面登录”下我发现页面登录需要不同类型的令牌...)

  • 从<手动获取令牌一个href="https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=manage_pages,publish_stream,offline_access&response_type=token" rel="nofollow">https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=manage_pages,publish_stream,offline_access&response_type=token
  • 在这种情况下,redirect_uri 可以是任何内容,我们只是想要发回的 url 令牌参数
  • 复制从 URL 发回的令牌(如果一切顺利,则过期url 参数在响应中设置为 0)
  • 然后我基本上做了这个 PHP 脚本:
require_once('facebook-sdk/facebook.php');
//Required facebook auth vars
$appID = 'APP_ID';
$appSecret = 'APP_SECRET';
$pageID = 'PAGE_ID';
$appOfflineToken = 'TOKEN_RETURNED_IN_PREVIOUS_STEP';
$pageTokenURL = 'https://graph.facebook.com/me/accounts?access_token=';
$access_token = '';

//connect to facebook app
$facebook = new Facebook(array(
    'appId'  => $appID,
    'secret' => $appSecret,
    'cookie' => true
));

//get page managed pages information
$jsonData = file_get_contents($pageTokenURL.$appOfflineToken);
$content = json_decode($jsonData, true);

//filter access_token for desired page using pageID
foreach($content['data'] as $item) {
    if($item['id'] == $pageID){
        $access_token = $item['access_token'];
        break;
    }
}

//format post
$post =  array(
    'access_token' => $access_token,
    'picture' => "http://URL_TO_PICTURE,
    'link' => "http://URL_TO_NEWS_POST",
    'name' => "NEWS_TITLE",
    'description' => 'NEWS_DESCRIPTION'
);

//post content to page wall
$res = $facebook->api('/'.$pageID.'/feed', 'POST', $post);

所以我的问题是......即使这看起来很牵强......它是正确的吗?好吧,至少它有效!

PS:对链接感到抱歉,但只能在这篇文章中提交 2 个...看起来没有足够的街头信誉:p

Ok so I finally managed to successfully link dynamic news posts on my website with my page's wall as Page (not user) through PHP.
Still I'm wondering if my solution is correct, or if it'll work on the long-run.
Having said that here's what I did:

  • Created a profile to administrate the page
  • Created the company page
  • Created an app with the domain pointing to my site domain, and website pointing to my website url
  • Disable offline_access deprecation in order to be able to issue offline_access tokens
  • Found out my pageID through http:// graph.facebook.com/PAGE_NAME
  • Went to https:// developers.facebook.com/tools/explorer/APP_ID
  • Paste the pageID in the instead of the userID and clicked submit
  • Then I clicked on get access token and checked manage_pages,publish_stream,offline_access

(when this alone didn't work, I visited https://developers.facebook.com/docs/authentication/ and under "Page Login" I found out that Page Login requires a different type of token...)

require_once('facebook-sdk/facebook.php');
//Required facebook auth vars
$appID = 'APP_ID';
$appSecret = 'APP_SECRET';
$pageID = 'PAGE_ID';
$appOfflineToken = 'TOKEN_RETURNED_IN_PREVIOUS_STEP';
$pageTokenURL = 'https://graph.facebook.com/me/accounts?access_token=';
$access_token = '';

//connect to facebook app
$facebook = new Facebook(array(
    'appId'  => $appID,
    'secret' => $appSecret,
    'cookie' => true
));

//get page managed pages information
$jsonData = file_get_contents($pageTokenURL.$appOfflineToken);
$content = json_decode($jsonData, true);

//filter access_token for desired page using pageID
foreach($content['data'] as $item) {
    if($item['id'] == $pageID){
        $access_token = $item['access_token'];
        break;
    }
}

//format post
$post =  array(
    'access_token' => $access_token,
    'picture' => "http://URL_TO_PICTURE,
    'link' => "http://URL_TO_NEWS_POST",
    'name' => "NEWS_TITLE",
    'description' => 'NEWS_DESCRIPTION'
);

//post content to page wall
$res = $facebook->api('/'.$pageID.'/feed', 'POST', $post);

so my question is ... even though this seems farfetched ... is it correct? well at least it works!

PS: sorry about the links but could only submit 2 in this post ... not enough street cred it seems :p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2025-01-14 00:14:01

你似乎是对的,但唯一的问题是它离线访问,

我在这工作了两周,只有我的问题离线访问
我想知道工作多少天或几周,因为在我的项目中,offline_access 工作两个小时
我的代码有问题还是什么?

You seems right but the only problem it offline_access

i am work in this from two weeks only my problem offline_access
i want to how many days or weeks works because when in my project the offline_access work for two hours
and is there problem in my code or what ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文