首页 文章

如何使用facebook php sdk v4和graph api 2.x在facebook页面墙上发布帖子作为管理员

提问于
浏览
5

如何使用facebook php sdk v4和graph api 2.x在Facebook页面墙上发布帖子作为页面管理员用户?

我花了很多时间,但大多数文章都很旧(旧的Facebook PHP SDK的例子),有点令人困惑

我已经找到了以下步骤

第1步:将应用程序重定向到Facebook以记录管理员
第2步:获取用户访问令牌(短期)
步骤3:通过交换短期令牌获取用户访问令牌(长期存在)
第4步:获取页面访问令牌

你能解释一下我需要为上面的步骤调用哪些Facebook PHP SDK的功能吗?你能给我一些代码示例的网址吗?

要么

如果我错了,请纠正我,谢谢你提前

=======================
注意:

  • 我已经为app_id和app_secret创建了应用程序

  • 我创建了Facebook页面

  • 我正在使用Facebook PHP SDK 4

  • Facebook PHP SDK 4使用图形API 2.x(我假设)

2 回答

  • 0

    我有一个tutorial that explains how to achieve posting to a page使用PHP SDK v4.0.x和Graph API v.x.

    基本上,您可以通过执行以下操作来获取页面访问令牌:

    // get page access token
    $access_token = (new FacebookRequest( $session, 'GET', '/' . $page_id,  array( 'fields' => 'access_token' ) ))
        ->execute()->getGraphObject()->asArray();
    
    // save access token in variable for later use  
    $access_token = $access_token['access_token'];
    

    然后,您可以使用我们在上面获得的访问令牌进行第二次API调用以将内容发布到给定页面:

    // post to page
    $page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
        'access_token' => $access_token,
        'name' => 'Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x',
        'link' => 'https://www.webniraj.com/2014/08/23/facebook-api-posting-as-a-page-using-graph-api-v2-x-and-php-sdk-4-0-x/',
        'caption' => 'The Facebook API lets you post to Pages you administrate via the API. This tutorial shows you how to achieve this using the Facebook PHP SDK v4.0.x and Graph API 2.x.',
        'message' => 'Check out my new blog post!',
      ) ))->execute()->getGraphObject()->asArray();
    
    // return post_id
    print_r( $page_post );
    
  • 2

    我找到了获得永不过期令牌的另一种方法:

    //get your access token for the app that you will use in 
        //https://developers.facebook.com/tools/explorer/
    
        //exchange it for long-term access token
    
        $accessToken='xxxxxxxxGet it above';
        $session = new FacebookSession($accessToken);
        $response = (new FacebookRequest($session, 'POST', '/oauth/access_token', array(
            'grant_type' => 'fb_exchange_token',
            'client_id' => 'the client id for the app',
            'client_secret' => 'the client secret for the app',
            'fb_exchange_token' => $accessToken)))->execute()->getGraphObject()->asArray();
    
    
        $LongTermUserAccessToken=$response['access_token'];
    
    
        $session = new FacebookSession($LongTermUserAccessToken);
    
        //get My User ID if you don't know 
    
        $response = (new FacebookRequest( $session, 'GET', '/me' ,  array( 'fields' => 'id' ) ))
            ->execute()->getGraphObject()->asArray();
        $meId=$response['id'];
    
        //get Tokes for all my pages the page that you are usig will have a never expire token
        //you can prove on https://developers.facebook.com/tools/debug/
    
        $response = (new FacebookRequest( $session, 'GET', '/'.$meId.'/accounts' ))
            ->execute()->getGraphObject()->asArray();
    
        var_dump($response);die;
    

相关问题