简体   繁体   中英

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

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.

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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