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
}
}
}