如何发布到Facebook页面(如何获取页面访问令牌+用户访问令牌) [英] How to post to a Facebook Page (how to get page access token + user access token)

查看:112
本文介绍了如何发布到Facebook页面(如何获取页面访问令牌+用户访问令牌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



测试时我收到一系列错误消息:
异常:200:用户尚未授权应用程序执行此操作



页面管理员已访问了应用程序并接受了以下权限:publish_stream,manage_pages,offline_access



以下是我计划使用的代码:

  //将页面管理员ID插入
//此用户与当前登录并使用该应用程序的用户不一样
//此用户是已授权的页面管理员:
// - manage_pages
// - offline_access
// - publish_stream

$ user_id ='123456789';

//在此插入页面ID
$ page_id ='123456789';

$ accounts = $ facebook-> api('/'.$ user_id。'/ accounts');

foreach($ accounts ['data'] as $ account)
{
if($ account ['id'] == $ page_id)
{
$ page_access_token = $ account ['access_token'];
echo< p>页面访问令牌:$ page_access_token< / p>;
}
}

//发布到您的页面上的墙上
尝试
{
$ result = $ facebook-> api (array(uid=> $ page_id,
method=>stream.publish,
access_token=> $ page_access_token,
message=> ; $ message,));
}
catch(FacebookApiException $ e)
{
error_log('FB错误:无法在页面墙上发布页面ID:'。$ page_id)
error_log('FB Error Message:'。$ e);
}

注意:上面的代码可能有PHP错误,因为我刚刚拼接它在飞行中,但它不是PHP错误我需要纠正,但更多的我的逻辑上理解我如何打算这个过程。



问题:
如果没有页面管理员的活动用户访问令牌,我无法访问$ user_id / accounts信息。



我试图实现的最终结果是:
1.)一个正常的FB用户去应用程序并提交一个表单
2.)应用程序在FB页面墙上发布消息,该页面不是FB用户所有,但之前已由页面管理员授权,具有以下权限manage_pages,publish_stream和offline_access



Q1。由于页面管理员已经接受了相应的权限,为什么不能生成活跃的用户访问令牌,而没有实际的Page Administrator用户登录到网站?



Q2 。有没有办法,当以不同的用户登录Facebook(这就是为什么我不使用/我/帐户)时,我可以获得与Page Administrator user_id相当的/ $ user_id / accounts?



Q3。请确认我的理解需要页面访问令牌发布到页面墙是正确的(或者我需要用户访问页面管理员访问_参见Q1)?



Q4。任何人都可以使用各种access_token实际访问的方便资源?



如果您需要更多信息,请告诉我们。
我花了最后几天工作,我被困住了。



谢谢!

解决方案


  1. 您可以向页面管理员询问 manage_pages 以及 offline_access 。我在生产应用程序中执行此操作,以便将安排的帖子发布到页面的墙壁上。


  2. 不。不可能。这就是要求权限的一切。为什么每个人都不能管理别人的页面。如果您可以管理任何人的页面,而不授予您访问权限,您可以形象吗?


  3. 要以页面的形式发布到页面,您需要一个页面访问令牌。要作为用户发布到页面的墙壁,您需要一个用户访问令牌。


  4. 是的,请参阅: https://developers.facebook.com/docs/reference/api/permissions https://developers.facebook.com/docs/authentication/


如果您对其中任何一个有其他疑问,请开始一个新问题。对于Stackoverflow的用户来说,在4个问题中遇到问题并不是很公平,然后被问及后续问题。


I am trying to work out how to post to a Facebook page wall, when using my app as a different Facebook User (who is not the Page Administrator).

I get a range of error messages while testing: Exception: 200: The user hasn't authorized the application to perform this action

The page administrator has visited the app and accepted the following permissions: publish_stream, manage_pages, offline_access

Here is the code I plan to use:

// Insert Page Administrators ID here 
// This user is not the same user that is currently logged in and using the app
// This user is the page administrator who has authorised: 
// - manage_pages
// - offline_access
// - publish_stream

$user_id = '123456789'; 

// Insert Page ID here
$page_id = '123456789'; 

$accounts = $facebook->api('/'.$user_id.'/accounts');

foreach($accounts['data'] as $account)
{
  if($account['id'] == $page_id)
  {
    $page_access_token = $account['access_token'];
    echo "<p>Page Access Token: $page_access_token</p>";
  }
}

// publish to the wall on your page
try
{
  $result = $facebook->api(array( "uid" => $page_id, 
                                  "method" => "stream.publish",
                                  "access_token" => $page_access_token,
                                  "message" => $message, ));
}
catch (FacebookApiException $e)
{
  error_log('FB Error: Could not post on Page Wall. Page ID: ' . $page_id);
  error_log('FB Error Message: ' . $e);
}

Note: There may be PHP errors in the code above, as I just spliced it on the fly, but its not so much the PHP errors I need correcting, but more my logically understanding of how I am meant to go about this process.

PROBLEM: I can't access the $user_id/accounts information without an active user access token for the Page Administrator.

The end result that I'm trying to achieve is: 1.) A normal FB user goes to the app and submits a form 2.) The app posts a message on a FB Page wall, which is not owned by the FB user, but has previously been authorized by the Page Administrator with the following permissions manage_pages, publish_stream and offline_access

Q1. Since the Page Administrator has accepted the appropriate permissions, why can't I just generate an active user access token, without the actual Page Administrator user logging into the website?

Q2. Is there a way I can get the equivalent of /$user_id/accounts for the Page Administrator user_id, when logged into Facebook as a different user (which is why I do not use /me/accounts)?

Q3. Please confirm that my understanding of needing the page access token to post to the page wall is correct (or do I need the user access_token for the Page Administrator - see Q1)?

Q4. Anyone have a handy resource on what each type of access_token can actually access?

If you need any more information, please let me know. I've spent the last few days working on this and I'm stuck.

Thanks!

解决方案

  1. You can ask the page admin for manage_pages along with offline_access. I do this in my production app to be able to post scheduled postings onto the pages' walls.

  2. Nope. Not possible. That's what asking permissions is all about. And why not everyone gets to administer everyone else's pages. Could you image if you could administer anyone's page without them granting you access?!?

  3. To post to the page as the page, you need a page access token. To post to page's wall as a user, you need a user access token.

  4. Yes, please see: https://developers.facebook.com/docs/reference/api/permissions and https://developers.facebook.com/docs/authentication/

If you have further questions about any one of these, please start a new question. It's not really fair to users of stackoverflow to be hit with 4 questions in one and then to be asked followup questions to each of those.

这篇关于如何发布到Facebook页面(如何获取页面访问令牌+用户访问令牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆