Copied to clipboard

Flag this post as spam?

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


  • Andreas 3 posts 73 karma points
    Jul 07, 2017 @ 09:34
    Andreas
    0

    Access token issue

    Hi! I am working on an MVC page and try to add Skybrud.Social 0.9.6 to get the support to get twitter, facebook and instagram feeds on the page. In the controller I have the following code.

    The twitter service works, but when I try to get posts and media from the facebook and instagram services I get the following exceptions. I am trying to use an app access token for facebook.

    What am I doing wrong? Shouldn't this generate the access tokens?

    Skybrud.Social.Instagram.Exceptions.InstagramOAuthAccessTokenException: 'The access_token provided is invalid.'

    Skybrud.Social.Facebook.Exceptions.FacebookException: 'An access token is required to request this resource.'

     if (currentBlock.Type == Vingruppen.Business.Shared.Enums.SocialMedia.Twitter)
            {
                TwitterOAuthClient client = new TwitterOAuthClient
                {
                    ConsumerKey = currentBlock.AppKeyOrId,
                    ConsumerSecret = currentBlock.AppSecret
                };
    
                TwitterService service = TwitterService.CreateFromOAuthClient(client);
    
                model.twitterFeed = service.Statuses.GetUserTimeline(currentBlock.UserName).Body;
    
            }
            else if (currentBlock.Type == Vingruppen.Business.Shared.Enums.SocialMedia.Facebook)
            {
                FacebookOAuthClient client = new FacebookOAuthClient(currentBlock.AppKeyOrId, currentBlock.AppSecret);
    
                //string accessToken = client.GetAppAccessToken();
    
                FacebookService service = FacebookService.CreateFromOAuthClient(client);
    
                model.facebookFeed = service.Posts.GetPosts(currentBlock.UserName).Body.Data;
    
            }
            else if (currentBlock.Type == Vingruppen.Business.Shared.Enums.SocialMedia.Instagram)
            {
                InstagramOAuthClient client = new InstagramOAuthClient(currentBlock.AppKeyOrId, currentBlock.AppSecret);
                InstagramService service = InstagramService.CreateFromOAuthClient(client);
    
                model.instagramFeed = service.Users.GetRecentMedia(int.Parse(currentBlock.UserName)).Body.Data;
            }
    
  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jul 09, 2017 @ 19:28
    Anders Bjerner
    0

    Hi Andreas,

    Facebook and Instagram works a bit different than Twitter, so the error message is actually spot on - you need to specify an access token.

    Your current code for Facebook and Instagram will not get you any access tokens.

    Facebook
    For Facebook, this is very straightforward, as we can get an access token by concatenating the app id and app secret. The code for that could look like:

    FacebookService service = FacebookService.CreateFromAccessToken(currentBlock.AppKeyOrId + "|" + currentBlock.AppSecret);
    

    The access token in this case will be an tied to your app (thus called an app access token), and therefore doesn't have access to private content on Facebook. This is probably fine in most cases, since you're most likely trying to fetch posts that are already public on Facebook. Otherwise, you can read more about user access tokens, which can have more permissions - you can read more about this here:

    http://social.skybrud.dk/facebook/authentication/user-access-token/

    Instagram
    Instagram doesn't support app access tokens - only user access tokens. This means you have to log in as a user to get an access token - eg. by setting up your own authentication page - you can read more about this here:

    http://social.skybrud.dk/instagram/authentication/setting-up-an-authentication-page/

    If you're using the Skybrud.Social Umbraco package (I can't see from your code whether that is the case), it also has a property editor for Instagram authentication you can use instead. It's lets a user (in the backoffice) log in with his/her Instagram account, and an access token for that user is then saved in the property.

    I hope that answers your questions ;)

  • Andreas 3 posts 73 karma points
    Jul 12, 2017 @ 06:44
    Andreas
    0

    Thank you!

    When I test the changes to the facebook code I get the following exception instead.

    FacebookService service = FacebookService.CreateFromAccessToken(currentBlock.AppKeyOrId + "|" + currentBlock.AppSecret);
    
    model.facebookFeed = service.Posts.GetPosts(currentBlock.PageName).Body.Data;
    

    Skybrud.Social.Facebook.Exceptions.FacebookException: '(#15) This method cannot be called with a client token or an app secret embedded in the client code'

    Do you need to have a user access token to get the posts from a public page?

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jul 12, 2017 @ 06:54
    Anders Bjerner
    0

    I'm not sure about the error, as the above works for me.

    Are you sure the page is public? Some pages requires users to be logged in with their Facebook account, but otherwise doesn't require further permissions. If this is the case, you won't be able to use an app access token.

    Anyways, Facebook has a development page that lists user and app access token for each of your app - you could try grabbing an app token and see if that works:

    https://developers.facebook.com/tools/accesstoken/

  • Andreas 3 posts 73 karma points
    Jul 12, 2017 @ 07:19
    Andreas
    0

    Thanks for your help. Apparently, the page is not public due to age restrictions.

Please Sign in or register to post replies

Write your reply to:

Draft