Facebook的SDK .NET - (#200)的用户没有授权的应用程序来执行此操作应用程序、操作、用户、SDK

2023-09-05 03:47:23 作者:浪得一身野气

我想发布消息到Facebook页面从C#Web应用程序。我收到以下异常抛出调用 FacebookClient.Post(...)

I am trying to post a message to a Facebook page from a C# web application. I am getting the following exception thrown on calling FacebookClient.Post(...):

FacebookOAuthException (OAuthException - #200)(#200),用户可以选择不   授权应用程序来执行此操作

FacebookOAuthException (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action

code:

var facebookClient = new FacebookClient();
facebookClient.AppId = appId;
facebookClient.AppSecret = appSecret;

if (Request["code"] == null)
{
    var authUrl = facebookClient.GetLoginUrl(new
    {
        client_id = appId,
        client_secret = appSecret,
        scope = "publish_stream",
        redirect_uri = Request.Url.AbsoluteUri
    });

    Response.Redirect(authUrl.AbsoluteUri);
}
else
{
    dynamic result = facebookClient.Get("oauth/access_token", new
    {
        client_id = appId,
        client_secret = appSecret,
        grant_type = "client_credentials",
        redirect_uri = Request.Url.AbsoluteUri,
        code = Request["code"]
    });

    facebookClient.AccessToken = result.access_token;
    // Store access token
}

发送消息:

protected void PublishMessage(string message)
{
    FacebookClient client = new FacebookClient(AccessToken);

    client.AppId = ApplicationId;
    client.AppSecret = ApplicationSecret;

    dynamic parameters = new ExpandoObject();
    parameters.message = message;

    client.Post(PageName + "/feed", parameters);
}

我接受以下Facebook的提示,以确保应用程序可以访问:

I accept the following Facebook prompts to ensure that the app has access:

在我的Facebook个人资料设置,应用程序是这样的:

And in my Facebook profile settings, the app looks like this:

我使用Facebook SDK的.NET v6.4.2(最新的)。

I am using the Facebook SDK for .NET v6.4.2 (the latest).

推荐答案

这看似愚蠢的问题,但不是你缺少你的访问令牌请求,请求code?难道不应该是这样吗?

This may seem a silly question, but aren't you missing the Request code from your access token request? Shouldn't it be like this?

dynamic result = facebookClient.Get("oauth/access_token", new
{
    client_id = appId,
    client_secret = appSecret,
    grant_type = "authorization_code"
    redirect_uri = Request.Url.AbsoluteUri,
    code = Request["code"]
});

修改刚刚意识到,必须使用请求code时使用不同的grant_type:)

Edit Just realized, you have to use a different grant_type when using the request code :)

 
精彩推荐
图片推荐