Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • keilo 566 posts 1021 karma points
    Aug 02, 2014 @ 21:54
    keilo
    0

    Facebook example

    Hi Anders

    Thank you for this great package.

    I am trying to test the Facebook example.cshtml in package's page resources tab.

    For facebook, created the App, got the API and the key. Keyed these in data-type's propertys along with return URL which points to http://siteaddres/App_Plugins/Skybrud.Social/Dialogs/FacebookOAuth.aspx

    Once added to document-type and in the page' I clicked Authorize, which brought the Facebook dialog. The dialog was showing in red-box the message "The following permissions have not been approved for use: read_stream, user_status, user_about_me, user_photos and user_activities.If you make your app public, they will not be shown to people using your app. Submit them for review or learn more." 

     

    If I proceed with it, it gets the ID (ID:xxxx) along with logo and expirty 59 days and I can save the document. But the example script doesnt output anything (put a <p>an entry</p> inside foreach loop) when I run from the partial. No errors, just the foreach loop not working as if it cant get any entries?

     

    I thought I need to make the Facebook app "Online", so under Facebook settings changed it Public (online).Went back to CMS, click Cancel, and Authorize again. This time it show the facebook login screen, upon logging-in, captured the ID and expiry date as 59 days (i.e. didnt show that red box notice). Tried the partial scrip and foreach loop not showing anything. ex: foreach (FacebookPostSummary post in response.Data) {<p>post.Caption</p>}

    What I am missing here? Any ideas/pointers would be greatly appreciated.

    Cheers!

     

     

     

     

     

     

     

     

     

     

  • Anders Bjerner 476 posts 2885 karma points MVP 6x admin c-trib
    Aug 02, 2014 @ 22:13
    Anders Bjerner
    0

    From what I can remember, the red warning is only shown to you since you're the administrator of the app. This is something Facebook have introduced after I wrote the authentication code, and I haven't had the the time to fully look into why it is shown, and if there can be done something so it won't show. But it shouldn't have anything do with the problem in your Razor file.

    The fact that you don't get any errors make it seem like things work as they should. If you check the Umbraco.config file (or something similar), there should be an access token token saved along with the other data in the property. If there is an access token, you can use Facebook's Graph Explorer to test it: https://developers.facebook.com/tools/explorer/

    Also, in the Razor file, are you sure the code doesn't generate some HTML? Even just an empty P-tag? Not all posts have a caption, so that might be the case. You can also check the amount of returned posts with this line:

    @response.Data.Length
    

    Let me know what you find out ;)

  • keilo 566 posts 1021 karma points
    Aug 02, 2014 @ 22:40
    keilo
    0

    Hi Anders!

    Yes no html does gets output inside the foreach loop.

    I have located the access token in umbraco.config; like (anonymized the values)

      <facebook><![CDATA[{

      "id": 12345677888,

      "name": "NAME HERE",

      "access_token": "THE-ACCESS-TOKEN-SHOWS-HERE",

      "expires_at": "2014-10-01 17:25:20",

      "scope": [

        "public_profile",

        "read_stream",

        "user_activities",

        "user_photos",

        "user_about_me",

        "user_status"

      ]

    }]]></facebook>

    With this If I go to Graph Explorer and key this in; (while logged in as the admin/owner user)

    /me/posts

    this returns the JSON:

    {

      "data": [

        {

          "id": "12345566", 

          "from": {

            "id": "3345345345", 

            "name": "xxxx"

          }, 

          "message": "xxxx ", 

          "story": "xxx", 

          "picture": "xxxxx", 

          "link": "xxxxxx", 

          "name": "Timeline Photos", 

          "caption": "xxxxxxxxxxx", 

          "properties": [

            {

              "name": "By", 

              "text": "xxx", 

              "href": "https://www.facebook.com/xxx"

            }

          ], 

     

    So it seems to work with Graph Explorer. Using the Facebook.cshtml if I put an html like <p>A record here..</p> the for loop does not execute at all.

    So i have updated your Facebook.cshtml code with this;

      <fieldset><legend>Facebook</legend>

                <p>@me.Name (ID: @me.Id)</p>

            </fieldset>

            // Gets the most recent posts of the authenticated user (me)

            FacebookPostsResponse response = service.Methods.GetPosts("me");

            <p>@response.Data.Length</p>

    The @me.Name and @me.ID shows as per the acquired.

    But @response.Data.Length shows 0.

    Strange isnt it? Not sure what is being missed....

  • Anders Bjerner 476 posts 2885 karma points MVP 6x admin c-trib
    Aug 02, 2014 @ 22:58
    Anders Bjerner
    0

    I haven't seen something like this before, so if you can try out the following things, we should hopefully find the answer.

    The Graph Explorer will initially have another access token than the one saved in Umbraco. Have you tried entering the access token from Umbraco? You can also try to replace FacebookService service = facebook.GetService(); with FacebookService service = FacebookService.CreateFromAccessToken("accessToken"); and then specify the access token from the Graph Explorer.

    Also, could you try calling this method service.Methods.Raw.GetPosts("me"). It returns a string, which is the raw JSON response returned by the Facebook API. Hopefully we can see there if anything goes wrong.

    You could also try replacing service.Methods.GetPosts("me") with service.Methods.GetPosts("umbraco"). The Umbraco-profile should have some posts.

  • keilo 566 posts 1021 karma points
    Aug 02, 2014 @ 23:16
    keilo
    0

    Yes, I have keyed in the token from umbraco.config to the Facebook Graph API exactly.

    Taking your pointer I have changed the facebook.cshtml code to

        // Get the OAuth information stored in the property

        FacebookOAuthData facebook = Model.GetPropertyValue("facebook") as FacebookOAuthData;

        // Check whether the OAuth data is valid

        if (facebook != null && facebook.IsValid) {

            // Gets an instance of FacebookService based on the OAuth data

            //FacebookService service = facebook.GetService();

    FacebookService service = FacebookService.CreateFromAccessToken("TOKEN-COPIED-FROM-GRAPH-API");

    <p>@service.Methods.Raw.GetPosts("me")</p>

    and this does output the entire RAW JSON.

    The part Im a little confused is I "did" enter the token from umbraco.config 'exactly' into the Facebook Graph API token field (as per my messsage earlier).

    So this approach seems to get the raw json just fine! Progress!! Now how do you advise to proceed from here, to iterate the posts etc.?

     

    I am not quite clear on what you meant by service.Methods.GetPosts("me") with service.Methods.GetPosts("umbraco")

    A little confused on this particular one : )

  • keilo 566 posts 1021 karma points
    Aug 02, 2014 @ 23:21
    keilo
    0

    Just a quick update.

    If I replace the line

    FacebookService service = FacebookService.CreateFromAccessToken("TOKEN-COPIED-FROM-GRAPH-API");

    with token from umbraco.Config it still outputs the RAW json.

    If i try after with the token from Graph API (little different, although i checked *only* the permissions stated)

    I still get the raw JSON output... 

    not sure if that is of any help?

    Cheers!

  • Anders Bjerner 476 posts 2885 karma points MVP 6x admin c-trib
    Aug 03, 2014 @ 00:26
    Anders Bjerner
    0

    Sorry if I didn't make this clear. I was talking about different things you could try.

    1. Changing the access tokens could reveal any issues with permissions, since the two access tokens might not have the same permissions. I'm not sure what you found out. Was the response the same for two access tokens?

    2. Replacing service.Methods.GetPosts("me") with service.Methods.Raw.GetPosts("me") would give you the raw JSON response, which we could check for any errors. Does the raw responses look the same if you test with the the two tokens?

    3. The last thing with replacing service.Methods.GetPosts("me") with service.Methods.GetPosts("umbraco") was to test whether you could retrieve posts from the umbraco profile. me is referring to the authenticated user.

  • keilo 566 posts 1021 karma points
    Aug 03, 2014 @ 08:30
    keilo
    0

    Hi again Anders

    Gave it a try again with a clear mind.

    For clarification, I have tried both the token in umbraco.config and the one I got from Facebook Graph API (for FBgraphAPI I used the exact permissions i.e. read_stream, user_status, user_about_me, user_photos and user_activities )

    lf I run the following code (one with token from umbraco.config and other with facebook graph API), both seems to return a JSON which I can see from service.Methods.Raw.GetPosts("me") so it seem to get some (i have included snippets from each below).

    But then it simply doesnt iterate the posts foreach, no html outputs;

    //umbraco acquired token

    //FacebookService service = FacebookService.CreateFromAccessToken("UMBRACO-CAPTURED-TOKEN");

    // facebook-graph-api acquired token

    FacebookService service = FacebookService.CreateFromAccessToken("FACEBOOK-GRAPH-GENERATED-TOKEN-WITH-SAME-PERMS");

                  @* This shows JSON in both cases, although the JSON is a bit different *@

    <textarea>@service.Methods.Raw.GetPosts("me")</textarea>

            FacebookMeResponse me = service.Methods.Me();

              <fieldset>

                <legend>Facebook</legend>

                <p>@me.Name (ID: @me.Id)</p>

            </fieldset>

            // Get the most recent posts of the authenticated user (me)

    @* CASE 2 *@

            FacebookPostsResponse response = service.Methods.GetPosts("me");

    @* CASE 3 *@

            //FacebookPostsResponse response = service.Methods.GetPosts("umbraco");

            <p>@response.Data.Length</p>

     

            foreach (FacebookPostSummary post in response.Data) {

                <p>There is a post</p>

                

     

            }

    With above no HTML outputs with  CASE 2;  FacebookPostsResponse response = service.Methods.GetPosts("me");
    But if comment it out and activate CASE3; FacebookPostsResponse response = service.Methods.GetPosts("umbraco");
    Then the foreach iterates 3 times..(?)
    So is it not getting the posts in the JSON? Is there a specific thing I need to do when I create the App under Facebook? I did try making it a public App with no joy..
    Not sure whats that Im missing as "umbraco" seems to iterate but even with generated token I cant seem to iterate the foreach at all..

    The JSON i got via the Umbraco.config token looks like (followed by facebook graph api acquired JSON below it);
       "data":[
          {
             "id":"123456788",
             "from":{
                "id":"263430000",
                "name":"Test Acccount"
             },
             "story":"Test Account likes a post.",
             "story_tags":{
                "0":[
                   {
                      "id":"12345777",
                      "name":"Test Account",
                      "offset":0,
                      "length":10,
                      "type":"user"
                   }
                ]
             },
             "privacy":{
                "value":""
             },
             "type":"status",
             "created_time":"2013-09-06T06:25:10+0000",
             "updated_time":"2013-09-06T06:25:10+0000"
          },
    ..... more enries here...
    ...
    Graph-API-acquired JSON looks like;
     "data":[
          {
             "id":"123456788",
             "from":{
                "id":"263430000",
                "name":"Test Acccount"
             },
             "message":"Test post here.Click on this link for more details. ",
             "story":"The story title comes here.",
             "picture":"https:\/\/fbcdn-sphotos-c-a.akamaihd.net\/werwer461632798_n.jpg?oh=g3yh45h4h4&oe=54555FE3&__gda__=14862_fafd345c5b120",
             "link":"https:\/\/www.facebook.com\/anotheraccount/asdas57\/?type=1",
             "name":"Timeline Photos",
             "caption":"Test Account2 is now open!\n\nLorem ipsum dolor sit amet.",
             "properties":[
                {
                   "name":"By",
                   "text":"TestAccount2",
                   "href":"https:\/\/www.facebook.com\/anotheraccount?ref=stream"
                }
             ],
             "icon":"https:\/\/fbstatic-a.akamaihd.net\/rsrc.php\/testacct.gif",
             "actions":[
                {
                   "name":"Comment",
                   "link":"https:\/\/www.facebook.com\/2345356\/posts\/33456465"
                },
                {
                   "name":"Like",
                   "link":"https:\/\/www.facebook.com\/223453453\/posts\/2345345"
                }
             ],
             "privacy":{
                "description":"Your friends of friends",
                "value":"FRIENDS_OF_FRIENDS",
                "friends":"",
                "networks":"",
                "allow":"",
                "deny":""
             },
             "type":"photo",
             "status_type":"shared_story",
             "object_id":"5654567567",
             "application":{
                "name":"Photos",
                "id":"76353564"
             },
             "created_time":"2013-06-03T08:26:13+0000",
             "updated_time":"2013-06-03T08:26:13+0000"
          },
  • Anders Bjerner 476 posts 2885 karma points MVP 6x admin c-trib
    Aug 03, 2014 @ 17:51
    Anders Bjerner
    100

    After doing some further research, I have now figured out what goes wrong. And honestly I can't believe I haven't found this before while testing.

    When calling the service.Methods.GetPosts("me") method, it will actually get the photos for the specified user, and not the posts as intended. So that is why your posts are not showing up.

    I have fixed the issue in the code at GitHub, but I'm not ready to make a new release yet. So meanwhile you can do something like this:

    // Gets the most recent posts of the authenticated user (me)
    FacebookPostsResponse response = FacebookPostsResponse.ParseJson(service.Methods.Raw.GetPosts("me"));
    
    foreach (FacebookPostSummary post in response.Data) {
    
        <p>@post.Id</p>
    
    }
    

    This should hopefully solve your issues ;)

  • keilo 566 posts 1021 karma points
    Aug 04, 2014 @ 09:29
    keilo
    0

    Glad the mystery solved! That explains a lot why I was seeing profile pictures when I was digging the JSON file : )

    Not that it works, I have few questions

    1) If the facebook account have multiple profiles (apparently organisation account can have multiple), where they maintain the content only on a specific profile, can I use the FacebookPostsResponse like this:

    FacebookPostsResponse response =FacebookPostsResponse.ParseJson(service.Methods.Raw.GetPosts("profileName"));

    and will this effect the 60 day token I receive when I authorize via skybrudFacebook as I am not sure if that gives only "me" the rights, or rights to query profiles within the facebook account?

    2) Within the foreach loop if I do @post.Caption I get the text where the links etc. are returned as text (I presume its the same issue like twitter without the helper function?). Is there a particular method(s) where one can render the basic presentation of the post inside the foreach; like caption, image, likes. A simple example would help greatly for ones not using Visual Studio.

    3) Is there a way to limit the number of posts to process in foreach? Tried .Take(x) without success?

     

    Many thanks!

  • Anders Bjerner 476 posts 2885 karma points MVP 6x admin c-trib
    Aug 04, 2014 @ 13:25
    Anders Bjerner
    0

    1) Generally organizations have a public profile, where posts are public as well. So if you authenticate with your own user, you should be able to get posts from the organization as well.

    2) The Facebook API has some support for this, but it is something they have added after I wrote most of the implementation for Facebook. I haven't had the time to look into this, so it is currently supported in Skybrud.Social.

    3) response.Data is a normal array, so you should be able to call response.Data.Take(5), but the Take method is an extension method from the System.Linq namespace. If you add @using System.Linq to the top of your Razor file, it should work ;)

  • egoisticum 5 posts 53 karma points
    Aug 20, 2014 @ 18:52
    egoisticum
    0

    Hi Anders and all others interested in this topic.

    My issue is:

    1. I'm pretty new in facebok API's

    2. I just need make functionality with loging for users with facebook (so, that is why I'm writting on this place)

    3. I installed Skybrud.Social plugin, registered on fb developers section and just got developer credentials

    4. I made new datatype and added it on "LoginFacebook" document type, after what I prepared "LoginFacebook" template and copied your code from this link: https://github.com/abjerner/Skybrud.Social.Umbraco/blob/master/Property%20Editors/Facebook%20OAuth.md

    5. I also added new content node with the same name "LoginFacebook" and authorised myself successfully.

    6. I saved and published new content and just got an error.

    7. I debuged it and just got an error "not initialize" was on first line of code: FacebookOAuthDatafacebook=Model.GetPropertyValue("facebook")asFacebookOAuthData;

    Hmmmmm... To be sure that I explained you pretty well, I'm going to copy my code here:

    @using Skybrud.Social.Facebook

    @using Skybrud.Social.Facebook.Objects

    @using Skybrud.Social.Facebook.Responses

    @using Skybrud.Social.Umbraco.Facebook.PropertyEditors.OAuth

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    @{

        Layout = "Master.cshtml";

    }

    @{

        // Get the OAuth information stored in the property

        FacebookOAuthData facebook = Model.GetPropertyValue("facebook") as FacebookOAuthData;

        // Check whether the OAuth data is valid

        if (facebook != null && facebook.IsValid) {

            // Gets an instance of FacebookService based on the OAuth data

            FacebookService service = facebook.GetService();

            FacebookMeResponse me = service.Methods.Me();

            <fieldset>

                <legend>Facebook</legend>

                <p>@me.Name (ID: @me.Id)</p>

            </fieldset>

            // Gets the most recent posts of the authenticated user (me)

            FacebookPostsResponse response = service.Methods.GetPosts("me");

            foreach (FacebookPostSummary post in response.Data) {

                // Do something with each post

            }

        }

    }

     

    After all... I'm not sure if it was an error because of plugin or because of my lack of knowledge about umbraco cms in generall. I was not sure if it was because of Model rendering and Page rendering methods in generall. I also tried to create partial view as you mentioned on your example and called it like @{ Html.RenderPartial("umbFacebookLogin");} but still not success. 

     

    If you can figure out where I'm making mistake you will make my day (even week) uncredible better.

    Please solve my doubts.

    Thank you in advance.

    Of course, if you will need any addition description of problem, I'm just waiting for your request... ;)

Please Sign in or register to post replies

Write your reply to:

Draft