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(); 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)必须使用活动访问令牌来查询有关当前用户的信息。

请参阅此处: (OAuthException – #2500)必须使用活动访问令牌来查询有关当前用户的信息

您将获得APPLICATION的访问令牌,而不是用户。 因此,“我”没有意义。 您应该在那里提供ID – 您的用户ID,或您的应用ID,或您的应用拥有权限的任何其他ID。

 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响应。 由于这个原因,当使用FacebookClient.Get(“oauth / access_token”)时,“oauth / access_token”令牌将不起作用。 相反,您需要在FacebookOAuthClient中使用方法。

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

希望这可以帮助。