Creating facebook apprequests

你离开我真会死。 提交于 2019-12-22 08:14:28

问题


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

I don't know where is the problem.


回答1:


You should read the requests documentation.
In there is an explination about two different types of requests.

  • user initiated (with the request dialog )
  • app generated (with the Graph API)

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.




回答2:


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'.

To answer

  1. You need email and read_stream permissions of the user. Monitor his wall using the RealTime Updates and then email him using your server SMTP.

  2. See http://developers.facebook.com/docs/reference/api/event/#invited on how to create an event invite

  3. As @Lix pointed out, see https://developers.facebook.com/docs/channels/#requests

  4. You should accomplish this using the new Open Graph object/actions. See this example: https://developers.facebook.com/docs/beta/opengraph/tutorial/




回答3:


You can receive Facebook app access token via:

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 = 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);


来源:https://stackoverflow.com/questions/8862621/creating-facebook-apprequests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!