简体   繁体   English

创建Facebook apprequests

[英]Creating facebook apprequests

I'm trying to send a message from an app to a user when a specific event happens. 我正在尝试在特定事件发生时从应用程序向用户发送消息。 Right now I have this code 现在我有这个代码

$param = array(
   'message'      => 'XYZ shared a file with you',
   'data'         => 'additiona_string_data',
   'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/$uid/apprequests", "POST", $param);

but I always get Uncaught OAuthException: (#2) Failed to create any app request thrown 但我总是得到Uncaught OAuthException: (#2) Failed to create any app request thrown

I don't know where is the problem. 我不知道问题出在哪里。

You should read the requests documentation. 您应该阅读请求文档。
In there is an explination about two different types of requests. 有关两种不同类型的请求的探索。

What you need is the app generated requests , that means you'll need the apps access token and not the users. 您需要的是应用程序生成的请求 ,这意味着您将需要应用程序访问令牌而不是用户。

I assume that you are using the users access token because you did not include the initiation of the facebook object in your code sample and have probably verified the user already hence the getAccessToken() call will return the users access token and not the applications access token. 我假设您正在使用用户访问令牌,因为您没有在代码示例中包含facebook对象的启动,并且可能已经验证了用户,因此getAccessToken()调用将返回用户访问令牌而不是应用程序访问令牌。

I'm a little confused as to "I'm trying to send a message from an app to a user when a specific event happens. Right now I have this code" means. 我有点困惑“我正在尝试在特定事件发生时从应用程序向用户发送消息。现在我有这个代码”的意思。

  1. Sending an email to a user when someone posts on their wall 当有人在墙上发帖时向用户发送电子邮件

  2. Sending an event invite to a user 向用户发送活动邀请

  3. Sending an app invite to a user 向用户发送应用邀请

  4. Writing on a users wall when something happens like 'XYZ shared a file with you'. 在发生类似'XYZ与您共享文件'之类的事情时,在用户墙上写字。

To answer 回答

  1. You need email and read_stream permissions of the user. 您需要用户的emailread_stream权限。 Monitor his wall using the RealTime Updates and then email him using your server SMTP. 使用RealTime更新监视他的墙,然后使用您的服务器SMTP向他发送电子邮件。

  2. See http://developers.facebook.com/docs/reference/api/event/#invited on how to create an event invite 有关如何创建活动邀请,请参阅http://developers.facebook.com/docs/reference/api/event/#invited

  3. As @Lix pointed out, see https://developers.facebook.com/docs/channels/#requests 正如@Lix指出的那样,请参阅https://developers.facebook.com/docs/channels/#requests

  4. You should accomplish this using the new Open Graph object/actions. 您应该使用新的Open Graph对象/操作来完成此操作。 See this example: https://developers.facebook.com/docs/beta/opengraph/tutorial/ 请参阅此示例: https//developers.facebook.com/docs/beta/opengraph/tutorial/

You can receive Facebook app access token via: 您可以通过以下方式接收Facebook应用访问令牌

https://graph.facebook.com/oauth/access_token?client_id=FB_APP_ID&client_secret=FB_APP_SECRET&grant_type=client_credentials

Working code sample to post app-to-user request using Facebook PHP SDK (add error handling where required): 使用Facebook PHP SDK发布应用程序到用户请求的工作代码示例(在需要时添加错误处理):

$facebook = new Facebook(array(
  'appId'  => FB_APP_ID,
  'secret' => FB_APP_SECRET,
));

$token_url = "https://graph.facebook.com/oauth/access_token?" ."client_id=" . 
   FB_APP_ID ."&client_secret=" . FB_APP_SECRET ."&grant_type=client_credentials";

$result = file_get_contents($token_url);
$splt = explode('=', $result);
$app_access_token =$splt[1];

$facebook->setAccessToken($app_access_token);

$args = array(
    'message' => 'MESSAGE_TEXT',

);
$result = $facebook->api('/USER_ID/apprequests','POST', $args);

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

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