Facebook C#SDK无法获取我/帐户

时间:2012-04-19 20:57:04

标签: c# facebook sdk facebook-c#-sdk

我正在尝试编写一个Windows服务,该服务将在运行时发布到我的Facebook页面并显示结果。

我刚刚下载了Facebook C#SDK v6.0.10.0并在.Net 4.0中编写了Windows应用程序

我创建了一个facebook应用程序帐户,并获得了所需的AppID和密码。

最终目标是将这个Windows服务发布在我的Facebook页面墙上作为页面而不是应用程序用户。

当我去获取我的Facebook应用程序的帐户时,我一直收到错误。

string strAppID = "my app api id";
string strSecret = "my app secret code";
Facebook.FacebookClient fbClient = new Facebook.FacebookClient();
fbClient.AppId = strAppID;
fbClient.AppSecret = strSecret;
dynamic ac = fbClient.Get("oauth/access_token", new
{
    client_id = strAppID,
    client_secret = strSecret,
    grant_type = "client_credentials"
});

string strAccessToken = String.Empty;
strAccessToken = ac.access_token;
if (!String.IsNullOrEmpty(strAccessToken))
{

    fbClient = new Facebook.FacebookClient(strAccessToken);
    fbClient.AccessToken = strAccessToken;
    fbClient.AppId = strAppID;
    fbClient.AppSecret = strSecret;

    //Here is where it is bombing
    dynamic fbAccounts = fbClient.Get("/me/accounts");

    fbClient = new Facebook.FacebookClient(strAccessToken);
    fbClient.AccessToken = strAccessToken;
    fbClient.AppId = strAppID;
    fbClient.AppSecret = strSecret;

    dynamic me = fbClient.Get("**Name of the facebook page I am trying to post to**");

    string strPageID = String.Empty;
    strPageID = me.id;

    string strPageAccessToken = String.Empty;

    //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID)
    foreach (dynamic account in fbAccounts.data)
    {
        if (account.id == strPageID)
        {
            //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out.
            strPageAccessToken = account.access_token;
            break;
        }
    }


    try
    {
        fbClient.AccessToken = strPageAccessToken;
        var args = new Dictionary<string, object>();
        args["message"] = "Testing 123";
        fbClient.Post("/" + strPageID + "/feed", args);

    }
    catch (Facebook.FacebookOAuthException ex)
    {
        // oauth exception occurred
    }
    catch (Facebook.FacebookApiLimitException ex)
    {
        // api limit exception occurred.
    }
    catch (Facebook.FacebookApiException ex)
    {
        // other general facebook api exception
    }
    catch (Exception ex)
    {
        // non-facebook exception such as no internet connection.
    }
}

我得到的错误就行了:

dynamic fbAccounts = fbClient.Get("/me/accounts");

(OAuthException - #2500)必须使用活动访问令牌来查询有关当前用户的信息。

2 个答案:

答案 0 :(得分:1)

见这里:(OAuthException - #2500) An active access token must be used to query information about the current user

  

您正在获取APPLICATION的访问令牌,而不是用户。   因此,“我”没有意义。你应该在那里提供身份证 -   要么是您的用户ID,要么是您的应用ID,或者您的应用拥有的任何其他ID   。的权限。

答案 1 :(得分:0)

dynamic ac = fbClient.Get("oauth/access_token", new
{
    client_id = strAppID,
    client_secret = strSecret,
    grant_type = "client_credentials"
});

上述代码可能不适用于6.0版。

  

OAuth 2.0 - 访问令牌的交换代码

     

FacebookClient仅支持解析json响应。由于此   原因“oauth / access_token”令牌在使用时不起作用   FacebookClient.Get(“OAuth的/的access_token”)。相反,你需要使用   FacebookOAuthClient中的方法。

您可以在此处找到更多详细信息:http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx

希望这有帮助。