首页 文章

Facebook C#SDK将页面发布到页面

提问于
浏览
3

我一直在努力让Facebook C#SDK发布到我的页面,作为页面几天 .

从我的谷歌搜索,我发现该过程应如下:

我可以通过图形资源管理器跟踪此过程,它可以工作 . 使用生成的令牌在页面上创建帖子 .

如何使用C#SDK执行此操作?

我试过了:

dynamic userTokenResult = client.Get("oauth/access_token", new
{
    client_id = appid,
    client_secret = appsecret,
    grant_type = "client_credentials"
});

dynamic longLivedResult = client.Get("oauth/access_token", new
{
    client_id = appid,
    client_secret = appsecret,
    grant_type = "fb_exchange_token",
    fb_exchange_token = userTokenResult.access_token;
});

client.AccessToken = longLivedResult.access_token;

// Post the message
dynamic messagePost = new
{
    link = message.LinkUrl,
    name = message.LinkName,
    caption = message.LinkCaption,
    description = message.LinkDescription,
    message = message.Message
};

// Set the status
var postId = client.Post("pagename/feed", messagePost);

但是,我怀疑这是返回应用程序access_token,而不是用户access_token(它在GET:me / accounts时失败) .

2 回答

  • 3

    您无法从服务器端代码获取用户令牌(即使您知道登录名/密码) . 你应该:

    • 从Graph API Explorer中复制/粘贴它

    • 从JS SDK客户端获取它

    • 使用FacebookClient中的GetLoginUrl函数获取登录URL并将用户重定向到该页面 . 登录完成后,Facebook将重新调用您的功能 - 在该功能中,您将能够使用该令牌 . 下面是我的MVC项目中的两个函数(授权和回调) - 但我认为你会得到这个想法 .

    public ActionResult Authorize(Guid eventId)
     {
    
    var redirectUri = ConfigurationProvider.HostingEndpoint + this.Url.Action("AuthorizeCallback", new { eventCode = eventId });
    
    var service = new FacebookClient();
    var loginUrl = service.GetLoginUrl(new {
        client_id = ConfigurationProvider.FacebookAppId,
        client_secret = ConfigurationProvider.FacebookAppSecret,
        redirect_uri = redirectUri,
        response_type = "code",
        scope = "manage_pages, publish_actions, user_photos, publish_stream" // Add other permissions as needed
    });
    
    return new RedirectResult(loginUrl.AbsoluteUri, permanent: false);
    }
    

    这会将用户重定向到Facebook登录页面 . 当用户输入凭据并按下登录时,将调用此函数(注意 code 参数 - 它将用于获取令牌):

    public ActionResult AuthorizeCallback(string code, string eventCode)
    {
    
        var redirectUri = ConfigurationProvider.HostingEndpoint + this.Url.Action("AuthorizeCallback", new { eventCode = eventId });
    
        var fb = new FacebookClient();
        dynamic result = fb.Post("oauth/access_token", new
        {
            client_id = ConfigurationProvider.FacebookAppId,
            client_secret = ConfigurationProvider.FacebookAppSecret,
            redirect_uri = redirectUri,
            code = code
        });
    
        var accessToken = result.access_token;
    
        // update the facebook client with the access token so 
        // we can make requests on behalf of the user
        fb.AccessToken = accessToken;
    
        // now get externded app Token
        dynamic extendedToken = fb.Get("oauth/access_token", new
        {
            client_id = ConfigurationProvider.FacebookAppId,
            client_secret = ConfigurationProvider.FacebookAppSecret,
            grant_type = "fb_exchange_token",
            fb_exchange_token = fb.AccessToken
        });
    
    
        // Get the user's information
        dynamic me = fb.Get("me");
    }
    

    之后,您应该调用“ /me/accounts ”,找到您的页面并从那里获取其令牌 .

  • 0

    如果您只是想发布到自己的页面,另一种方法是使用Windows PowerShell和http://facebookpsmodule.codeplex.com . 这会将操作减少到几行PowerShell脚本 .

相关问题