実行時に結果を Facebook ページに投稿する Windows サービスを作成しようとしています。
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) 現在のユーザーに関する情報を照会するには、アクティブなアクセス トークンを使用する必要があります。