Copied to clipboard

Flag this post as spam?

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


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

    Problems with setting-up facebook login

    Hi Dear, please sorry me because of I'm openning new topic here.

    The thing is: I tried to repply on one of the opened but I could not.

    So, my original, and I think simple question was:

    ===================================================================

    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 answer me anything.

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 20, 2014 @ 23:39
    Anders Bjerner
    0

    I'm not sure that I understand what the error is.

    Am I correct to assume that everything works in the backoffice, but something goes wrong while rendering the view/partial in the frontend? If that is the case, do you see a YSOD (yellow error page)? This will most likely contain information about the error, so if you can post a screenshot of that, I'll have a look at it ;)

  • egoisticum 5 posts 53 karma points
    Aug 21, 2014 @ 09:57
    egoisticum
    0

    1st of all... Thanks for quick response and sorry because I was not concrete enough.

    Here is the screenshoot of YSOD.

    The thing is... I simply can not declare "FacebookOauthData" variable and initiate it from "Model.GetPropertyValue("facebook")"...

    I was also waching on umbraco temporary files which is presenting Model classes, but didn't figured out anything...

    To solve your doubts: Yes, indeed, it works in backoffice with my fb credentials perfect which is not the case with my code in partial view from your facebook example from documentation.

    It would be much easier for me if you can explain us where are you storing fb App data which we needed to put in umbraco Skybrud.Social.Facebook datatype?

    For the end... I don't know if it could be important information for you, but... I'm not using Visual Studio Development Server, also not using IIS expres, I installed local version of IIS on my WIN 7 (32 bit)...

    If you'll need more info just let me know, and thank you in advance.

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 21, 2014 @ 10:06
    Anders Bjerner
    0

    In your content type, what is the name/alias of the Facebook property? The property alias specified in the frontend should be the one specified on your content type. From what I can tell, you're doing everything else correctly, so My guess is that the aliases doesn't match.

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

  • egoisticum 5 posts 53 karma points
    Aug 21, 2014 @ 11:08
    egoisticum
    0

    Hei, I checked it even before you mentioned, everything is OK with alias, but Model still can not find that property.

    To make things easier for you I'm going to describe you my site structure:

    1. There is one master page with header and footer

    2. There is also Login page for "basic-custom" (our own) login functionalities

    3. There is one more "LoginFacebook" subpage which is nested by Login page

    4. In "LoginFacebook" page I added facebook property with the "facebook" alias. So, it is not mistake as I see...

    If I can explain something more just let me know.

  • egoisticum 5 posts 53 karma points
    Aug 21, 2014 @ 12:34
    egoisticum
    0

    Dear Anders, it's again me... I finally fixed the error.

    The thing is... I observed whole properties from my site with "Model.Properties" and noticed that umbraco didn't genereate property from "FacebookLoginPage".

    After that, I tried to put facebook property on the top of my site (on Home Page) and it works now...

    Want to tell sorry you for westing your time, but in the same time, you helped me a lot.

    As I fixed first issue, I'm going back to work where I hope that I'll need again help. :)

    Good luck with developing your plugin ;)

  • Almir Dodigović 9 posts 55 karma points
    Aug 21, 2014 @ 23:30
    Almir Dodigović
    0

    On the end of working day I realised that I make all functionalities with integrating facebook user data and my system but I figured out that I used Authorisation button from backoffice during whole testing time for my app.

    Maybe this looks like stupid question, but still... How you "call" facebook Login button?

    I mean... Is there some methods in framework to make it custom through c# code or to use fb SDK?

    If I missed something, sorry me again. :)

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 21, 2014 @ 23:40
    Anders Bjerner
    0

    I'm not sure I understand your question.

    Are you trying to implement your own login in the frontend? Then this little gist might help you out ;)

    https://gist.github.com/abjerner/70c1b2af2b3ea480e353

  • Almir Dodigović 9 posts 55 karma points
    Aug 22, 2014 @ 10:32
    Almir Dodigović
    0

    Hi Anders.

    Indeed, you helped me a lot, but I still didn't get facebook login page.

    So, here is my logic...

    1. I'm working website using MVC render control

    In Model, I've creted this Model-View class:

        public class LoginViewFacebookModel
        {
            [DisplayName("Email")]
            public string Email { get; set; }
        }

    Next, I created RenderController which is checking is user logged in. The logic with RenderController is very simple:

    If user is successefully logged in, I'm getting Email from it's and checking if email address is currently saved in database.

    Depence of is Email storred in db or not I'm adding it or updating it in db.

    So, that is all OK for me, and thanks are going to you because of help. ;)

    Currently, I just made HandleController action in Controllers and that is my problem:

    I'm getting an error from facebook with this obvious message: "The redirect_uri URL must be absolute", the fb URL is: "https://www.facebook.com/dialog/oauth?client_id=274283822773136&redirect_uri=/&state=5a5714fd-6d87-47fb-88db-59dcca994f1f&scope=read_stream,publish_stream"

    To explain it more clear, I'm going to copy you my HandleController code:

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult HandleLoginFacebook(LoginViewFacebookModel model)
            {
                // Get the redirect URI (if present)
                string redirect = (Request.QueryString["redirect"] ?? "/");
                /*If user is not logged in => return facebook login button
                 * 1st: Get the FacebookOauthClient how we could access to appId, appSecret, and returnUrl*/
                FacebookOAuthClient oauth = new FacebookOAuthClient
                {
                    AppId = "274283822773136",
                    AppSecret = "a6e88675f06430d380e126418d4e11f9",
                    ReturnUri = redirect
                };

                // Set the state
                string state = Guid.NewGuid().ToString();
                // Construct the authorization URL
                string authorizatioUrl = oauth.GetAuthorizationUrl(
                state,
                FacebookScope.ReadStream + FacebookScope.PublishStream
                );
                // Redirect the user to the OAuth dialog
                return Redirect(authorizatioUrl);
            }

    Also, there is pretty basic .cshtml partialview with this code:

    @model LoginViewFacebookModel

    @if (!Model.Email.IsNullOrWhiteSpace())
    {
        <p>USER JE LOGIRAN!</p>
        <div class="form-horizontal">
            <div class="row view">
                <div class="form-group">
                    @Html.LabelFor(model => model.Email, "E-mail adresa", new { @class = "col-md-3 control-label" })
                    <div class="col-md-9">
                        @Html.TextBoxFor(model => model.Email, new { @class = "form-control", placeholder = DictionaryHelper.GetDictItem("PlaceHolderFacebookEmail", "Email adresa") })
                    </div>
                    @Html.ValidationMessageFor(model => model.Email, string.Empty, new { @class = "help-block" })
                </div>
            </div>
        </div>
    }
    else
    {
        using (Html.BeginUmbracoForm<AuthSurfaceController>("HandleLoginFacebook"))
        {
        @Html.AntiForgeryToken()
           
        <p>USER NIJE LOGIRAN!</p>
        <button type="submit" class="col-md-12 button">Login</button>
        }
    }

    If you'll need more info, just let me know.

  • Almir Dodigović 9 posts 55 karma points
    Aug 22, 2014 @ 17:11
    Almir Dodigović
    0

    It's again me. :) I fixed problem with the sending absolutePath. The thing is. I didn't know that, maybe it could be useful for somebody. When your activity from controller posting something to get past you should use "Request.UrlReferrer.AbsoluteUri", but when you are Getting something than use: "Request.Url.AbsoluteUri". That spent me a lot of time.

    OK, currently I have "some kind" of login, but that is still not what I wanted.

    So, I have few questions for you for which I think that you will not need that much time:

    1. I want to get from Skybrud.Social.Facebook datatype prevalues which we putted in backoffice no metter if I'm logged in or not. The thing is... I need FacebookOauthData variable even if there is no logged user. I saw it is in "cmsdatatypeprevalues" table in umbraco database. How to get it?
    2. How to create scope of permissions through c# code when you are creating Login button? I want to have permissions for email, about_me, etc... In general, is it possible to manipulate with it's permissions and how?
    3. Once when I got access to information "about_me" from facebook, how to get access it? I'm curious about c# code?

    Thank you in advance.

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 24, 2014 @ 01:12
    Anders Bjerner
    0

    Hi again,

    I don't have that much experience with MVC controllers, so I don't think I will be able to help you much there.

    Regarding the questions in your last post:

    1. The prevalues are not intented for use in the frontend. If you need the app id and app secret in the frontend, the best solution would probably be to hardcode them in your app settings, and then grab them from there.

    2. I'm not sure how your code looks, but have a look at this line. This is the code used for the authentication done in the backend. I don't have a complete list of available scopes, but you should be able to find more information about this in the Facebook developer documentation. You can also read more about obtaining a user access token in the Skybrud.Social documentation.

    3. Again I'm sure what you mean here? Do you want to get information about the authenticated user? This can be done calling the service.Methods.Me() method. You can see more about this here.

  • Almir Dodigović 9 posts 55 karma points
    Aug 25, 2014 @ 16:13
    Almir Dodigović
    0

    First of all thank you for provided examples. Now I figured out that I have the another one issue which could be even bigger than previous question of permissions.

    The thing is... When I'm logged into backoffice everything is working fine, but in case when I'm not logged in backoffice plugin is not recognize me as a logged user (I can not declare FacebookOauthData variable) even if I made logging proccess in facebook login page and finished sucessfully logging process and fb returned the data.

    In one word: Plugin is always using my backoffice logged user, how to force it to use our custom one?

    Can you please provide some example(s) where you are checking if user is logged in(...) through C# code?

    I hope I was clear enough, but if not, just let me know.

    And of course, thank you once again for answering all my questions. Thank you for understanding.

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 25, 2014 @ 16:44
    Anders Bjerner
    0

    Please notice the different between Skybrud.Social for Umbraco and Skybrud.Social.

    Skybrud.Social for Umbraco is package, and the purpose is to let admins/editors log in with their Facebook account via the backoffice. This package isn't targeted your visitors, but handles the backoffice integration between Skybrud.Social and Umbraco.

    Skybrud.Social on the other hand is the underlying framework used by the package.

    If you're using Skybrud.Social for Umbraco, you can make calls to the Facebook API based on the information entered in the backoffice. Also notice that the class FacebookOAuthData is part of this package, not the underlying framework.

    But if I understand your correctly, you're making some sort of login in the frontend for your visitors? In that case, you will probably only need Skybrud.Social, but not Skybrud.Social for Umbraco.

    If you already have made the steps for authentication and obtaining an access token, you should be able to do something like this:

    // Initialize a new instance of the FacebookService class
    FacebookService service = FacebookService.CreateFromAccessToken("theAccessTokenOfTheUser");
    
    // Get information about the authenticated user
    FacebookMeResponse me = service.Methods.Me();
    

    The value of me could be stored in the session of the user, and if present, it can be assumed that the user is logged in.

  • Almir Dodigović 9 posts 55 karma points
    Aug 25, 2014 @ 17:00
    Almir Dodigović
    0

    Very simple question: From which variable I can get userAccessToken?

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 25, 2014 @ 17:22
    Anders Bjerner
    0

    That really depends on your current code. You can see this example for earlier:

    https://gist.github.com/abjerner/70c1b2af2b3ea480e353#file-oauth-aspx-cs-L64

    Line 64 shows you how to obtain the access token during the login with Facebook. The access token could also be saved to the user session if you need it later during that session.

  • Almir Dodigović 9 posts 55 karma points
    Aug 26, 2014 @ 10:05
    Almir Dodigović
    0

    Hi again... Believe or not, it's me... :)

    OK... This is what I have done until now:

    1. I've created facebook Button with authorization url which is working OK. I also set permissions (facebook scope) "email" but I didn't saw any changes on login except that email is in url... Whatever, here is the code of that part: /* 1st: Get the FacebookOauthClient how we could access to appId, appSecret, and returnUrl*/ FacebookOAuthClient oauth = GetFacebookDefaultCredentials();

              // Set the state
              string state = Guid.NewGuid().ToString();
              // Construct the authorization URL
              string authorizatioUrl = oauth.GetAuthorizationUrl(
              state, "read_stream", "email", "user_status", "user_about_me", "user_photos"
              );
              return authorizatioUrl;
      
    2. I successfully checking if user is logged in or not and on this place I need it's facebook email address. I was hoping that I could find Email address on FacebookResponse variable, but it is simply not there. So, here is the rest of the code: /Creating facebook client just to get facebook Application credentials (AppId, AppSecret)/ FacebookOAuthClient facebookClient = FacebookHelper.GetFacebookDefaultCredentials();

              /*Creating and initialize Facebook Service*/
              string code = Request.QueryString["code"];
      
      
      
          // Exchange the auth code for an access token
          string accessToken = facebookClient.GetAccessTokenFromAuthCode(code);
      
      
          /*In case that user is not logged in we will set these properties: IsLogged, AuthorizationUrl*/
          if (accessToken == null)
          {
              /*As user is not logged in we will set property on false*/
              loggedFacebookUser.IsUserLogged = false;
      
      
              string redirect = (Request.QueryString["redirect"] ?? Request.Url.AbsoluteUri);
              /*FB login authorization URL initialize*/
              loggedFacebookUser.AuthorizationUrl = FacebookHelper.GetFacebookLoginPageUrl(redirect);
          }
          else
          {
              //Getting the FacebookService
              FacebookService facebookService = FacebookService.CreateFromAccessToken(accessToken);
      
      
              /*Gets the information about the authenticated user*/
              FacebookMeResponse facebookMe = facebookService.Methods.Me();
      
      
          }
      

      So, as you can see, I'm almost done, but as you know how it is... Without Email address I simply don't have anything.

    3. Beside of email address I need "About me" information from Logged user facebook profile. Indeed, I was seeking on facebook documentation, and I figured out that I should use "about_me" scope permision to get this info, but I don't know where to find it through c# code.

    At the end... I hope you will not give up from "my project" because I'm aware how much this can be frustrating, but on the other hand, still hope that you understand this kind of "beginners bug". Thank you in advance.

  • Almir Dodigović 9 posts 55 karma points
    Aug 26, 2014 @ 12:37
    Almir Dodigović
    0

    Hi there, after 2 hour of trying different combination, I found solution for an email property.

    The thing is, Email property is not existing in the FacebookMeResponse, but it exist in FacebookUser class and works perfect.

    This is the piece of code: //Getting the FacebookService FacebookService facebookService = FacebookService.CreateFromAccessToken(accessToken);

                    /*Gets the information about the authenticated user*/
                    FacebookMeResponse facebookMe = facebookService.Methods.Me();
    
                    FacebookUser fbUser = facebookService.Methods.GetUser(facebookMe.Id);
    

    So, now... I have just one more doubt, how to access to "User's about info", which represent short user Biography on facebook?

  • Almir Dodigović 9 posts 55 karma points
    Aug 26, 2014 @ 15:08
    Almir Dodigović
    0

    After one more day, solution became to me just like that... :) I figured out whole procedure which was so frustrating for me. When I'll polish all details I'll share my MVC code and whole idea about MVC implementation, hope it could be useful for somebody.

    After we finish this scrum sprint in our company, I'll try to implement also twitter and if it shows as good practice share MVC code to community.

    For the end... Dear Anders, I wish I could give you hundreds of "High Five!". This framework is really so powerful, and it will be pity if you'll not continue with developing it. I hope for your good, that it could became even commercial thing one day.

    So, for now... I'm out of this topic, but will be back so soon.

    Thanks once again. ;)

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Aug 27, 2014 @ 00:15
    Anders Bjerner
    0

    Hi again and thanks for the feedback

    I'm sorry that the documentation doesn't make this clear, but the FacebookMeResponse class doesn't expose the email address in a property like it should do (which currently doesn't have a release date). I have added the property in the code on GitHub, and it will be part of the next release of Skybrud.Social. Meanwhile this little hack/snippet might help you to obtain the email address: https://gist.github.com/abjerner/c2fe4ef2bea5afa025ea

    I'm not sure what you mean about the commercial thing? It is kind if you think people ought to pay a little for the package, but one of the purposes of Skybrud.Social is to give something back, since there are a lot of free packages that I use myself, so it will remain this way in the future as well ;)

  • ruihorta 3 posts 23 karma points
    Nov 06, 2014 @ 17:08
    ruihorta
    0

    Hi there. 

    Can someone explain me how to get:

    string code = Request.QueryString["code"];

    string action = Request.QueryString["do"];

    I'm running a umbraco project trying to use facebook api, but I have no idea where to get those parameters. My facebook app has the url: http://localhost/

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 18:40
    Anders Bjerner
    0

    Hi ruihorta,

    Can you elaborate a bit on what you're trying to do?

    If you're trying to implement your own login using Facebook, this is out of the scope of this package, but have a look at this gist: https://gist.github.com/abjerner/70c1b2af2b3ea480e353

  • ruihorta 3 posts 23 karma points
    Nov 07, 2014 @ 11:15
    ruihorta
    0

    Ty for the quick answer. I'm trying to implement facebook login in the frontend to use their API.

    I'm a little confused with the facebook login. I don't know how to correctly redirect to use these querystrings:

    string code = Request.QueryString["code"];
    stringaction=Request.QueryString["do"]; 

     

    Where do I place the redirects? The user is already logged in.

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Nov 07, 2014 @ 11:48
    Anders Bjerner
    0

    In my example, when the user first visits the login page, he will be presented with a login button:

    https://gist.github.com/abjerner/70c1b2af2b3ea480e353#file-oauth-aspx-cs-L40

    If your login page has the URL /login, clicking the login button will send the user to /login?do=login, which again will redirect the user to the Facebook login page.

    If the user completes the login at Facebook.com, he will be redirected back to /login?code={authorization code provided by Facebook}. The example code will then exchange the authorization code with an access token. The access token can then be used to make calls to the Facebook API.

  • ruihorta 3 posts 23 karma points
    Nov 07, 2014 @ 12:16
    ruihorta
    0

    Hmm. Is this code compatible with MVC? 
    I'm working with umbraco v7. I'm new to this stuff.

    This example is a class that is called in every page? Where do I put this class?

    Ty for your help. 

     

    Another thing. This "Content", what is the namespace that I should use?

    Content.Text += "Login with Facebook";

     And this method:

    protectedvoidPage_Load(objectsender,EventArgse)

    How is this supposed to be called? It's protected. And the arguments? What are they?

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Nov 07, 2014 @ 12:55
    Anders Bjerner
    0

    The example Gist is written in Web Forms, but it is roughly the same for MVC. The example is a login page, so you don't have to call it on every page.

    You can have a look at the Dialogue package which has implemented Facebook login using Skybrud.Social with MVC: https://github.com/leen3o/Dialogue/blob/a4a4a6ce03108455b7fb6909434dad785c03a8bb/Src/Dialogue.Logic/Controllers/OAuthControllers/FacebookOAuthSurfaceController.cs

    It is not something I have written, but it might help you out ;)

  • Omer 1 post 71 karma points
    Jan 18, 2016 @ 13:35
    Omer
    0

    Hi Anders Bjerner,

    I'm using Umbraco 7.4, and I've installed this plugin but after authorization I got this error. enter image description here

    Does this plugin supports Umbraco 7.4 version ?

Please Sign in or register to post replies

Write your reply to:

Draft