Paul

Paul

  • 0
  • 13
  • 0

C# Post on Facebook Page Error: (OAuthException - #200)

Feb 9 2014 9:27 PM
Hello,
 
I created a web page in Visual Studio 2005 in order to post messages on my Facebook Page and i'm using .Net Framework 4.0.
I downloaded and added as a reference in my project:
- Facebook 6.4.2
- System.Dynamic.Dll
- Newtonsoft.Json.dll (for .net 4.0)

When i try to run my project i got the following error message: (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action
What i'm doing wrong ? Can you help me to fix the error ?
Thank you
Paul
 
Here's my C# Code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Specialized;
using System.Dynamic;
using Facebook;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public partial class fb : System.Web.UI.Page
{
private const string FacebookAppId = "123456789012";
private const string FacebookAppSecret = "b9s7xkd8d88a8dc9v9vlx9s8kcv9c0s8";
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,user_photos,publish_checkins,photo_upload";
protected void Page_Load(object sender, EventArgs e)
{
string accessToken = GetAccessToken(FacebookAppId, FacebookAppSecret);
PostMessage(accessToken, "Test message");
}
private 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");
return accessToken;
}
private void PostMessage(string accessToken, string p_message)
{
try
{
FacebookClient facebookClient = new FacebookClient(accessToken);
var args = new Dictionary<string, object>();
args["message"] = p_message;
args["caption"] = String.Empty; // "Caption";
args["description"] = "test description";
args["name"] = "Name";
//args["picture"] = "";
//args["link"] = "http://www.google.com";
args["req_perms"] = "publish_stream";
args["scope"] = "publish_stream";
args["access_token"] = accessToken;
//Then pass your destination ID and target along with FB Post info. You're Done.
dynamic publishedResponse = facebookClient.Post("/" + PAGE_ID + "/feed", args);
}
catch (FacebookOAuthException ex)
{
//handle something
Response.Write("FacebookOAuthException = " + ex.Message.ToString() +"<br />");
}
catch (Exception ex)
{
//handle something else
Response.Write("Exception = " + ex.Message.ToString() + "<br />");
}
}
}


Answers (1)