For facebook fans only with asp.net c # sdk

Hi, I am developing a facebook application with C # sdk and I want a user who liked my page to be able to use only my application. (Like a woobox )

I found some solutions in php in this link , but there is no source about .net, how can I get the information I like in ASP.NET

I again find some examples in php in this link, but I cannot find the C # answer: \

thanks

+6
source share
5 answers

You receive a signed request when your web page loads in the facebook canvas application; you should be able to parse the signed request like this:

if (Request.Params["signed_request"] != null) { string payload = Request.Params["signed_request"].Split('.')[1]; var encoding = new UTF8Encoding(); var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/'); var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '=')); var json = encoding.GetString(base64JsonArray); var o = JObject.Parse(json); var lPid = Convert.ToString(o.SelectToken("page.id")).Replace("\"", ""); var lLiked = Convert.ToString(o.SelectToken("page.liked")).Replace("\"", ""); var lUserId= Convert.ToString(o.SelectToken("user_id")).Replace("\"", ""); } 

You need to add a link to json libraries to parse a signed request in C #, download from http://json.codeplex.com/

Also refer to How to decode OAuth 2.0 for Canvas signed_request in C #? if you are worried about a signed request.

+10
source

This is only possible with legacy APIs or with user_likes permission. Since you want a solution without specific permissions, I will show you 2 methods. Use them in conjunction with AJAX to refresh the page when the user clicks like .

Option 1) REST API

Using the deprecated API, you can use Pages.IsFan

 https://api.facebook.com/method/pages.isFan? page_id=...& uid=...& access_token=... 

Do it in C # as follows.

 var appID = "...."; var appSecret = "...."; var uid = "...."; var pageId = "...."; WebClient client = new WebClient(); var appAuthUri = string.Concat("https://graph.facebook.com/oauth/access_token?", "client_id=", appID, "&client_secret=", appSecret, "&grant_type=", "client_credentials" ); var response = client.DownloadString(appAuthUri); var access_token = response.Split('=')[1]; var isFanUri = string.Concat("https://api.facebook.com/method/pages.isFan?", "format=", "json", "&page_id=", pageId, "&uid=", uid, "&access_token=", access_token ); response = client.DownloadString(isFanUri); bool isFan; bool.TryParse(response, out isFan); 

Option 2) Client side

FBXML Method. This is done using Javascript on the client, subscribing to the event when the user clicks the like button. He documented here .

How do I know when a user clicks the Like button?

If you use the version of the XFBML button, you can subscribe to the "edge.create" event through FB.Event.subscribe.

Create the FBXML button as here .

 <div id="fb-root"></div> <script>(function(d){ var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js#appId=132240610207590&xfbml=1"; d.getElementsByTagName('head')[0].appendChild(js); }(document));</script> <div class="fb-like" data-href="http://www.thecodeking.co.uk" data-send="true" data-width="450" data-show-faces="false"></div> 

Then subscribe to the edge.create event using the Javascript SDK . Put this code in a BODY document, preferably just before it completes.

 <script type="text/javascript"> <!-- window.fbAsyncInit = function () { FB.init({ appId: '245693305442004', status: true, cookie: true, xfbml: true }); FB.Event.subscribe('edge.create', function (href, widget) { // Do something here alert('User just liked '+href); }); (function () { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); } ()); }; //--> </script> 
+4
source
 this.canvasAuthorizer = new CanvasAuthorizer { Permissions = new[] { "user_about_me", "publish_stream", "offline_access", "user_likes", "friends_about_me" } }; this.canvasAuthorizer.Authorize(); if (FacebookWebContext.Current.IsAuthorized()) { this.facebookWebClient = new FacebookWebClient(FacebookWebContext.Current); string requested_Data = HttpContext.Current.Request.Form["signed_request"]; dynamic decodedSignedRequest = FacebookSignedRequest.Parse(this.facebookApplication, requested_Data); if (decodedSignedRequest.Data.page != null) { // Funs Page this.IsLike = decodedSignedRequest.Data.page.liked; } else { // Application Page dynamic likes = this.facebookWebClient.Get("/me/likes"); foreach (dynamic like in likes.data) { if (like.id == this.FacebookFanPageID) { this.IsLike = true; } } } } 
+1
source

If your application is a canvas application, you can (should?) Use the signed_request parameter to check if the user likes the page it is on:

 # pseudocode signed_request = decode_signed_request() if signed_request['page']['liked']: # user liked page, do something cool else: # user doesn't like page. redirect somewhere to tell them why they should 

signed_request is passed to your page as a POST variable; just as if there was a form field called signed_request and the form was submitted on the previous page (in fact this is basically how facebook “launches” your application, the form is automatically submitted, and not waiting for the user imagine it). Therefore, in ASP.net you can get it through the Request object:

 Request["signed_request"] 

This approach is useful if you are creating a “tab” for a page; You can determine if the user liked the page without providing additional permissions.

+1
source

This can be done in PHP using SQL Query.

 `$result = $facebook->api(array( "method" => "fql.query", "query" => "SELECT uid FROM page_fan WHERE uid=$uid AND page_id=$page_id" )); 

Here the $ result variable can be used to separate the contents of Fan and non-Fan

0
source

Source: https://habr.com/ru/post/892122/


All Articles