Facebook C# SDK发布帖子到页面作为页面

3

我一直在努力让Facebook C# SDK发布我的页面,但是我已经试了几天。

从我的谷歌搜索中,我发现应该按照以下步骤进行:

  1. 使用我的用户帐户为manage_pagespublish_stream授权应用程序(通过此URL完成:https://graph.facebook.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=publish_stream,manage_pages
  2. 获取用户访问令牌
  3. 交换用户访问令牌以获取长期访问令牌
  4. 使用长期用户访问令牌获取me/accounts
  5. 从结果中获取页面访问令牌
  6. 使用长期访问令牌发布到page_id/feeds端点

我可以通过Graph Explorer遵循这个过程,并且它是有效的。页面上创建了一个带有生成令牌的帖子。

我如何使用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);

然而,我怀疑这返回的是应用程序访问令牌,而不是用户访问令牌(GET:me/accounts 失败)。
2个回答

3

无论您知道登录/密码,都不能从服务器端代码获取用户令牌。 您应该执行以下操作之一:

  1. copy/paste it from Graph API Explorer
  2. Get it from JS SDK client side
  3. Use GetLoginUrl function from FacebookClient to get login URL and redirect the user to that page. After login is completed, facebook will call your function back - and in that function you will be able to the the token. Below are 2 functions (authorize and callback) from my MVC project - but I think you will get the idea.

     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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接