TOP

系统集成Facebook授权发布帖子以及获取帖子评论等功能(二)
2019-09-04 00:57:41 】 浏览:169
Tags:系统集成 Facebook 授权 发布 帖子 以及 获取 评论 功能

access_expires_at'); 44 $metadata = [ 45 "app_id" => $appId, 46 "type"=> $type, 47 "user_id" => $userId, 48 "application" => $application, 49 "expires_at" => $expiresAt, 50 "is_valid" => $isValid 51 ]; 52 53 $auth = new Facebook\Authentication\AccessToken($accessToken); 54 if (! $auth->isLongLived()) { 55 try { 56 $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); 57 } catch (Facebook\Exceptions\FacebookSDKException $e) { 58 echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n"; 59 exit; 60 } 61 } 62 63      65 $this->cache->set($this->facebookAccessTokenKey,(string) $accessToken ); 66 $this->cache->set($this->facebookKey, json_encode($metadata)); 67 68 header("Location: {$callbackUrl}");  
 1   /**
 2      * 获取临时访问口令
 3      * @param $pageId
 4      * @param $access_token
 5      * @return mixed
 6      */
 7     public function getPageAccessToken($pageId, $access_token)
 8     {
 9         $accessTokenUrl = "https://graph.facebook.com/v4.0/{$pageId}?fields=access_token&access_token={$access_token}";
10         $pageAccessTokenInfo = curl($accessTokenUrl);
11         try{
12             $pageAccessTokenInfo = json_decode($pageAccessTokenInfo, true);
13             $pageAccessToken = $pageAccessTokenInfo['access_token'];
14         }catch (Exception $e) {
15             $this->showResults(-1, null, '获取page_access_token失败!');
16         }
17 
18         return $pageAccessToken;
19     }

唤起授权对话框,我们的业务权限是 manage_pages 和 publish_pages ,权限不同的自行更改!

   /**
     * 获取facebook授权code
     */
    public function bindAction(){
        $redirectUrl = facebook redirectUrl;
        $appId = facebook appId;
        $appSecret = facebook appSecret;
        $fb = new Facebook\Facebook([
            'app_id' => $appId,
            'app_secret' => $appSecret,
            'default_graph_version' => 'v2.10',
        ]);
        $helper = $fb->getRedirectLoginHelper();
        $permissions = ['manage_pages','publish_pages'];
        $codeUrl = $helper->getLoginUrl($redirectUrl, $permissions);
        $this->showResults(1, ['url' => $codeUrl]);
    }

完整的发帖代码

  /**
     * facebook分享发布
     */
    public function facebookShareAction()
    {
        $data = getPost('data');//一次请求中只能有一个选项  1,分享链接, 2单张图片链接, 3,多张图片ID
        $content['message'] = $data['message'];
        if ($data['link']) {
            $content['link'] = $data['link'];
        } else if ($data['url']) {
            $content['link'] = '';
            $content['url'] = $data['url'];
        } else if ($data['attached_media']) {
            $content['link'] = '';
            $content['url'] = '';

            $media = explode(',', $data['attached_media']);
            if (count($media) >= 10) {
                $this->showResults(-2, null, '一次上传最多不能超过9张图片!');
            }
            foreach ($media as $mediaId) {
                $temp[] = ['media_fbid' => $mediaId];
            }

            $content['attached_media'] = json_encode($temp);
        }

        $appId = facebook appId;
        $appSecret = facebook appSecret;
        $fbAccessToken = $this->cache->get($this->facebookAccessTokenKey);
        if (!$fbAccessToken) {
            $this->showResults(-1, null, '请先绑定Facebook账号!');
        }

        $fb = new Facebook\Facebook([
            'app_id' => $appId,
            'app_secret' => $appSecret,
            'default_graph_version' => 'v2.3',
        ]);

        try {
            $response = $fb->get(
                '/me/accounts',
                $fbAccessToken
  
		
系统集成Facebook授权发布帖子以及获取帖子评论等功能(二) https://www.cppentry.com/bencandy.php?fid=85&id=249956

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇文件上传漏洞 下一篇Yii2中$model->load($data)一..