简体   繁体   English

PHP Facebook API页面访问令牌

[英]Php Facebook API Page access tokens

I'm trying to build a plugin for question2answer (knowledge not necessary) where when a new question is posted, it posts that question as an activity on the facebook page. 我正在尝试为question2answer(无需知识)构建一个插件,在该插件中发布新问题时,会将其作为活动发布到facebook页面上。 Achieving this is not a problem. 实现这一点不是问题。 what I run into, is persistence. 我遇到的是持久性。

From what I understand, and please correct me if I'm wrong, is that you can only get access token directly for a page through the account of an admin user. 据我了解,如果我错了,请纠正我,因为您只能通过管理员用户的帐户直接获取页面的访问令牌。 This creates a problem for posting when the administrator of the page whose access token we've stored expires from logging out. 当我们已存储其访问令牌的页面管理员因注销而过期时,这将导致发布问题。 Well, we can't expect them to stay logged in forever, right? 好吧,我们不能指望他们永远保持登录状态,对吗?

Is there another way to go about this? 还有另一种解决方法吗? Like maybe getting the users to post on that page instead? 就像让用户在该页面上发帖一样?

Here is the code I am currently using (this is not final, no error handling). 这是我当前正在使用的代码(这不是最终的,没有错误处理)。 Anything that says qa_opt is just something stored in the question2answer database 任何说qa_opt的内容只是存储在question2answer数据库中的内容

$facebook2 = new Facebook(array(
                                                    'appId' => qa_opt('facebook_app_id'),
                                                    'secret' => qa_opt('facebook_secret')

                        ));

                        $facebook2->setAccessToken(qa_opt('facebook_page_access_code'));

                        // Try to extend token
                        $access_token = $facebook2->getExtendedAccessToken();



                        // As is, with the extended token, we currently post as the user, not the page. Let's fix that
                        $accounts = $facebook->api('/me/accounts?access_token='.$access_token);
                        foreach ( $accounts as $account )
                        {
                            if ( $account['id'] == qa_opt('facebook_page_id') )
                            {
                                $page_access_token = $account['access_token'];
                                break; // Stop processing foreach
                            }

                        }

                        $fbPageArgs = array('access_token' => $page_access_token,
                                    'message' => 'A new question has been created!',
                                    'link' => qa_q_path($params['postid'], $params['title'], true),
                                    'name' => $params['title'],
                                    'description' => $params['text'] 
                        );

                        $facebook2->api("/".qa_opt('facebook_page_id')."/feed?fields=access_token","post",$fbPageArgs);

You can have a never expiring token for your fan page, that will solve your problem i guess. 您可以为粉丝页面设置永不过期的令牌,我想这将解决您的问题。

Follow the simple steps: 遵循简单的步骤:

  1. Get the admin's(ie your's) extended token (2 months validity). 获取管理员(即您的)扩展令牌(有效期2个月)。 Go though the link to get the long-lived token . 通过链接获取long-lived token

    Extending tokens 扩展令牌

  2. Get the never expiring access token for any page using this token- 使用令牌获取任何页面的永不过期访问令牌-

     $facebook->api("/PAGE_ID?fields=access_token"); 

(You can use Facebook's Debug Tool to check the validity of the token). (您可以使用Facebook的调试工具来检查令牌的有效性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM