0
我有一个Windows服务,试图张贴到我的Facebook页面。下面的代码返回一个访问令牌,但是当我去发布消息时,它会抛出这个异常?我怎么能告诉我的脸书页面,这个应用程序可以发布到它?如何让我的Facebook应用程序访问发布到我的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
}
}
}