facebooksdk.net publish not working - can't get LoginButton.CurrentSession to publish to

时间秒杀一切 提交于 2019-12-02 14:01:43

问题


I've got what seems to be a pretty simple problem - I'm trying to use the facebooksdk.net to publish to a users feed.

First, I install with the following command, which gets me Facebook.Client version 0.9.91-alpha:

Install-Package Facebook.Client -pre

Next, I have a LoginButton (and I'm able to successfully login)

<fbc:LoginButton x:Name="_loginButton" 
SessionStateChanged="_loginButton_SessionStateChanged" Margin="0,15" />

Now, I want to post to the feed, so I use the following code, copied directly from the facebooksdk.net site for publishing:

private async void PublishStory()
{
    await _loginButton.RequestNewPermissions("publish_stream");
    var facebookClient = new Facebook.FacebookClient(_loginButton.CurrentSession.AccessToken);
    var postParams = new
        {
            name = this.PostTitle,
            message = _txtStatus.Text.Trim(),
            description = this.PostDescription.Replace(@"""", "'"),
            link = this.PostUrl,
            picture = this.PostImageUrl,
            actions = new {
                name = this.PostAppName,
                link = this.PostAppUrl,
            },
        };
    try
    {
        dynamic fbPostTaskResult = await facebookClient.PostTaskAsync("/me/feed", postParams);
        var result = (IDictionary<string, object>)fbPostTaskResult;

        var successMessageDialog = new Windows.UI.Popups.MessageDialog("Posted Open Graph Action, id: " + (string)result["id"]);
        await successMessageDialog.ShowAsync();
    }
    catch (Exception ex)
    {
        ShowLoginUI();
    }
}

The problem I'm running into is that _loginButton.CurrentSession cannot be resolved. Was it removed from this version of Facebook.Client, and if so, what's the right way to do this now?


回答1:


After some trial and error, I believe I've found the answer. The AccessToken is now accessible via Session.ActiveSession.CurrentAccessTokenData.AccessToken, which means the line of code should be:

var token = Session.ActiveSession.CurrentAccessTokenData.AccessToken;
var fbClient = new Facebook.FacebookClient(token);


来源:https://stackoverflow.com/questions/28310848/facebooksdk-net-publish-not-working-cant-get-loginbutton-currentsession-to-pu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!