我有一个试图发布到我的Facebook页面的Windows服务。以下命令返回一个访问令牌,但当我发布消息时,它会抛出此异常?如何告诉我的Facebook页面该应用程序可以发布到它?
(OAuthException - #200)(#200)用户未授权 应用程序执行此操作
这是主要的电话:
string accessToken = FacebookPoster.GetAccessToken("************", "***************");
FacebookPoster.PostMessage(accessToken, "My message");
这是我的FacebookPoster课程:
public class FacebookPoster
{
private const string AuthenticationUrlFormat =
"https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=manage_pages,offline_access,publish_stream";
public static string GetAccessToken(string apiId, string apiSecret)
{
string accessToken = string.Empty;
string url = string.Format(AuthenticationUrlFormat, apiId, apiSecret);
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
String responseString = reader.ReadToEnd();
NameValueCollection query = HttpUtility.ParseQueryString(responseString);
accessToken = query["access_token"];
}
if (accessToken.Trim().Length == 0)
throw new Exception("There is no Access Token");
// this returns an access token
return accessToken;
}
public static void PostMessage(string accessToken, string message)
{
try
{
FacebookClient facebookClient = new FacebookClient(accessToken);
dynamic messagePost = new ExpandoObject();
messagePost.access_token = accessToken;
//messagePost.picture = "[A_PICTURE]";
//messagePost.link = "[SOME_LINK]";
//messagePost.name = "[SOME_NAME]";
//messagePost.caption = "my caption";
messagePost.message = message;
//messagePost.description = "my description";
// this call throws the exception:
// (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action
var result = facebookClient.Post("/643028262411859/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
//handle something
}
catch (Exception ex)
{
//handle something else
}
}
}
答案 0 :(得分:0)
我发现了另一种方法
转到此处并单击图形资源管理器以生成访问令牌
https://developers.facebook.com/tools/
然后这段代码将创建帖子:
public static void PostMessage(string accessToken, string message)
{
try
{
FacebookClient facebookClient = new FacebookClient(accessToken);
dynamic getAccess = new ExpandoObject();
getAccess.access_token = accessToken;
dynamic accessResult = facebookClient.Get("/{your facebook userID}/accounts", getAccess);
foreach (dynamic j in accessResult.data)
{
if (j.name == "{Name of your facebook page}")
{
accessToken = j.access_token;
}
}
dynamic messagePost = new ExpandoObject();
messagePost.access_token = accessToken;
//messagePost.picture = "";
//messagePost.link = "[SOME_LINK]";
//messagePost.name = "[SOME_NAME]";
//messagePost.caption = "";
messagePost.message = message;
messagePost.url = "{url}";
// this will make a photo post
var result = facebookClient.Post("/{your page name}/photos", messagePost);
// this will make a regular post to your feed
facebookClient.Post("/{your page name}/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
//handle something
}
catch (Exception ex)
{
//handle something else
}
}