Is facebook user to user request possible with W8/

2019-01-15 21:02发布

问题:

I'm trying to get my mobile game to invite friends from facebook but I'm unable to. I'm using Facebook c# SDK for Windows 8 (hopefully porting when ready to WP8, Android and iOS) but the problem is I'm confused about the facebook API paremeters:

This is my code:

var fb = new FacebookClient (accessToken);
dynamic parameters = new ExpandoObject();
parameters.appId = facebookAppId;
parameters.message = "This is a test!";
parameters.to="friendID1,friendID2,...";

string request=String.Format ("/me/apprequests");
dynamic result = await fb.PostTaskAsync (request, parameters);

when I use this code, the Post task completes correctly, but (checking the result variable and looking at facebook) on the "me" user receives that invitation. The "to" field seems to be ignored and "me" is the only one receiving the request.

If I use

string request=String.Format ("/{0}/apprequests",FRIEND_ID);

instead of /me/apprequests, then I receive the following error:

OAuthException "(#2) Failed to create any app request",

I've read in some places that the access token must be an App Token, but the documentation says that it should only be used when your server is doing those requests, What I want is a user-to-user request, not an app-to-user request.

So far I've seen it's possible to get it in Android and iOS via a Dialog Request, but I've not found them in the C# SDK, so I'm trying to make the apprequest manually, without luck.

Is it possible to get this working with Windows Phone 8 or Windows 8? How?

Thanks, Kak

回答1:

Yes it is possible. Unfortunately, you cannot do it via the SDK since Facebook doesnt allow you to send requests programatically. It has to be done through the dialog request box/popup.

So, make sure the user is logged in before trying the following solution. After you get the facebook access token by using the LoginAsync method, follow these steps.

  1. Create a WebBrowser control in your xaml with script enabled.

<phone:WebBrowser Visibility="Collapsed" IsScriptEnabled="True" Name="FbBrowser"></phone:WebBrowser>

  1. Now in your code behind, create a string with the facebook ids of the users you want to invite in CSV format like "id1,id2,id3".

  2. Navigate the web browser to the following page and subscribe to the navigating event: "https://m.facebook.com/dialog/apprequests?" + "message=your_message&app_id=" + "your_facebook_appid" + "&redirect_uri=https://m.facebook.com&to=" + user_facebook_ids_CSV + "&sdk=2&display=touch"

FbBrowser.Visibility = Visibility.Visible; FbBrowser.Navigating += FbBrowser_Navigating; FbBrowser.Navigate(url1);

  1. You can handle the success and failure response of the webpage in the navigating event.

private void FbBrowser_Navigating(object sender, NavigatingEventArgs e) { if (e.Uri.ToString().StartsWith("https://m.facebook.com/?error_code") || e.Uri.ToString().StartsWith("https://m.facebook.com/?request")) { FbBrowser.Visibility = Visibility.Collapsed; FbBrowser.Navigate(new Uri("about:blank"));
} }