问题
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.
Sending an email to a user when someone posts on their wall
Sending an event invite to a user
Sending an app invite to a user
Writing on a users wall when something happens like 'XYZ shared a file with you'.
To answer
You need
email
andread_stream
permissions of the user. Monitor his wall using the RealTime Updates and then email him using your server SMTP.See http://developers.facebook.com/docs/reference/api/event/#invited on how to create an event invite
As @Lix pointed out, see https://developers.facebook.com/docs/channels/#requests
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