(OAuthException) (#200) User must have accepted TOS on C# - Facebook - c#

Well, I am trying make a app to write comments to facebook in C#.
Searching in google I know that I need an Application (I did it) and I need select the permissions. I did it..
Now I wrote my code in C#:
private string MyAppId = "XXX";
private string MyAppSecret = "XXX";
private void button1_Click(object sender, EventArgs e)
{
FacebookClient FB = new FacebookClient(MyAppId, MyAppSecret);
Dictionary<string,string> data = new Dictionary<string,string>();
data.Add("message","test");
FB.Post("OBJECT_ID/comments", data);
}
But when I click the button I get this error:
(OAuthException) (#200) User must have accepted TOS
I am getting crazy! Please help me =(

It doesn't look like you're actually using the users access token.
You need to go through the OAuth workflow, where the user is redirected to facebook.com and grants your application permission. Once that happens, you'll get an Access Token that you use to make requests on behalf of the user.
There's an overload for the FacebookClient class that will take an access token.
Since you didn't really expand on the type of app you're writing, the Facebook C# Github page has a collection of samples, for WinForms, ASP.NET, and Windows 8 Metro. This example should show you how to do client-side authentication.
You're also trying to post to OBJECT_ID, which isn't a valid user/post/page.

Related

Facebook SDK integration in WPF application

I have wpf desktop application and I want Facebook login integration in this application so that users can share images from local machine, moreover I am using "Facebook.7.0.6" sdk. Apparently I am facing following issue on login screen.
Given URL is not allowed by the Application configuration: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
And below coding I am using in my application.
private Uri GenerateLoginUrl(string appId, string extendedPermissions)
{
// for .net 3.5
// var parameters = new Dictionary<string,object>
// parameters["client_id"] = appId;
dynamic parameters = new ExpandoObject();
parameters.client_id = appId;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
// The requested response: an access token (token), an authorization code (code), or both (code token).
parameters.response_type = "token";
// list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
parameters.display = "popup";
// add the 'scope' parameter only if we have extendedPermissions.
if (!string.IsNullOrWhiteSpace(extendedPermissions))
parameters.scope = extendedPermissions;
// generate the login url
var fb = new FacebookClient();
return fb.GetLoginUrl(parameters);
}
void facebookBrowser_Navigated(Object sender,NavigationEventArgs e)
{
var fb = new FacebookClient();
FacebookOAuthResult oauthResult;
if (!fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
return;
if (oauthResult.IsSuccess)
LoginSucceeded(oauthResult);
}
Note : Let me know if Facebook have any change in term and condition for desktop application.
Thanks
After some study I got this link and now my application working fine.
Please set below settings on Facebook app first.
Native or desktop app? - Yes
Client OAuth login - Yes
Embedded browser OAuth Login - Yes
read more from this link :-https://www.hackviking.com/2014/11/facebook-api-login-flow-for-desktop-application/
Thanks
This error means you haven't configured well you app on facebook
If you are testing on localhost, you need to add a platform to your app, then configuring the "site url" for the example http://localhost. Then create a test app (a copy of your main app) and use it for your tests.

Using Facebook API to access public group event data

I'm developing a public website and what I want to do is pretty straightforward, but I'm pulling my hair out trying to get everything working right.
I administer an open Facebook group and I want to display the public facebook events of this group on my website.
I can't seem to figure out how to setup my authentication so that I can access the event data. Here is my code for using my application to get an auth token:
var fb = new FacebookClientWrapper();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = AppSettings.AppID,
client_secret = AppSettings.AppSecret,
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
I know this works fine because I can access some information - for example, if I access a specific event by its ID, I can retrieve that information.
The problem occurs when I try to retrieve a list of events with fields within a date range:
[HttpGet]
public object GetEventDetails(string unixStartDateTime, string unixEndDateTime)
{
var parms = new Dictionary<string, object>();
parms.Add("fields", new[] { "id","name","description","start_time","venue" });
if (!String.IsNullOrEmpty(unixStartDateTime)) { parms.Add("since", unixStartDateTime); }
if (!String.IsNullOrEmpty(unixEndDateTime)) { parms.Add("until", unixEndDateTime); }
var eventsLink = String.Format(#"/{0}/events", AppSettings.GroupID);
return ObjectFactory.GetInstance<IFacebookClient>().Get(eventsLink,parms);
}
(I'm aware that even if this did succeed, the return value wouldn't be serializable - I'm not concerned about that quite yet).
This GET request returns the following message:
(OAuthException - #102) A user access token is required to request this resource.
So the message is quite clear: I need a user access token to get the data I've requested. The question is - what is the best way to do this? Can I give my application a certain permission to read this data? I've looked over all the permissions available to apps, but I don't see one that would do the trick.
I don't want to require people to log onto Facebook to look at public event data, and I love the idea of allowing people with no technical experience to essentially update the website content by posting Facebook events to the group. Right now, I have to duplicate anything they do.
I would think this kind of application would be very common, but no matter what I've read or tried, I can't quite find an example of the same thing that works.
From the docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/group/events you need
A user access token for a member of the group with user_groups permission.
To avoid the hassle, you could create such an Access Token via the Graph Explorer and then store it in your application. Remember to exchange that Access Token to a long-lived one (https://developers.facebook.com/docs/facebook-login/access-tokens/#extending), and that you have to renew the Access Token every 60 days afterwards.

Tweeting on a user's behalf in asp.net

I have been searching for the most current method for posting a tweet on behalf of a user in Webforms. Most of the information I've come across dates to around 2010 and involves Twitterizer, which is no longer supported by the Twitter API. My question is, is there any updated documentation or examples, tutorials on the subject?
I've created my app, have the consumer key and secret, but most of the code I'm coming across is in php. Any help would be appreciated.
Since you're using WebForms (via your reply in comments), here's an example of tweeting on another user's behalf with LINQ to Twitter. Other examples might show you how to add a signature to an authorization header, but you'll still have to manage the OAuth workflow. This should give you an idea of how that workflow can be managed in WebForms.
LINQ to Twitter uses different authorizers to manage the process of producing OAuth signatures, managing credentials, and supporting OAuth workflow. First, instantiate a WebAuthorizer, like this:
public partial class _Default : System.Web.UI.Page
{
private WebAuthorizer auth;
private TwitterContext twitterCtx;
protected void Page_Load(object sender, EventArgs e)
{
IOAuthCredentials credentials = new SessionStateCredentials();
if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
{
credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
}
auth = new WebAuthorizer
{
Credentials = credentials,
PerformRedirect = authUrl => Response.Redirect(authUrl)
};
The WebAuthorizer only needs your ConsumerKey and ConsumerSecret, which can be saved in web.config. The authorization process is divided into two parts because you have to send the user to Twitter to authorize your app and then Twitter redirects the user back to your page to collect the other two tokens, which are oauth_token and access_token. That means you need logic to handle the callback from Twitter, which could look like this:
if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
{
auth.CompleteAuthorization(Request.Url);
}
This goes after you instantiate WebAuthorizer and makes sure you're processing a Twitter callback before performing completion. After you call CompleteAuthorize, go into auth.Credentials and grab the new user credentials and save them for the logged in user. On subsequent queries, you can then load all 4 credentials into WebAuthorizer and LINQ to Twitter will work without requiring the user to authorize your application again.
After you have credentials, you can instantiate a TwitterContext, which gives you access to the Twitter API. Here's an example that does that and performs a query:
if (auth.IsAuthorized)
{
twitterCtx = new TwitterContext(auth);
var search =
(from srch in twitterCtx.Search
where srch.Type == SearchType.Search &&
srch.Query == "LINQ to Twitter"
select srch)
.SingleOrDefault();
TwitterListView.DataSource = search.Statuses;
TwitterListView.DataBind();
}
This code follows the call to auth.CompleteAuthorize to make sure all credentials are populated. The auth.IsAuthorized verifies that all 4 credentials are present.
That was the completion and instantiation of the TwitterContext part, but you'll first need to start the oauth process. Here's a button click handler that does that:
protected void authorizeTwitterButton_Click(object sender, EventArgs e)
{
auth.BeginAuthorization(Request.Url);
}
Just call BeginAuthorization, which executes the callback assigned to the PerformRedirect property of WebAuthorizer, sending the user to Twitter to authorize your app. As mentioned earlier, Twitter redirects the user back to your page and CompleteAuthorization executes to finish the authorization process. I typically put the OAuth logic on a separate page to simplify things.
Once the user authorizes your app, you can execute any query you want, such as the method below that tweets some text for the user:
protected void postUpdateButton_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
twitterCtx.UpdateStatus(updateBox.Text);
updateBox.Text = string.Empty;
}
Tip: the SessionStateCredentials stores credentials in session state. So, you want to make sure you're using state server, SQL server, but definitely not InProc.
There's documentation on the LINQ to Twitter site at CodePlex.com and a working demo in the LinqToTwitterWebFormsDemo in the downloadable source code.

Facebook access token problem

I am connecting to login facebook page through an url. I receive the access token in my application and i can prints all my contacts from the list. I have a problem: there are times when i do receive the access token and if i logout from facebook and rebuild my application the second time , i don't have any access token. WHY? If i wait i guess 10-15 minutes and try again it works. How to resolve this? THX
I am using the auth url. THe following link was my example link:
http://geekdeck.com/vb-net-facebook-get-access-token-for-desktop-application/
EDIT:
I have the following code:
browserFacebook.Navigate(#"https://graph.facebook.com/oauth/authorize?client_id="+ FacebookApplicationID + "&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup");
string someString = browserFacebook.Url.ToString();
This returns something like the following:
"http://www.facebook.com/connect/login_success.html#access_token=ACCESS TOKEN.expires_in=0"
I can then easily use this access token with the Graph API to access an users facebook details as in the following code:
Facebook.FacebookGraphAPI g = new FacebookGraphAPI("ACCESS_TOKEN");
var fbUser = g.GetObject("me", null);
PROBLEM:
When I rebuild the application, the link that i receive is OpenDNS (or navigation to the webpage was canceled) and I have to access token. Why? How can I resolve this error? After a a while 1-2 hours I receive again the token.
"https://graph.facebook.com/oauth/authorize?client_id=" +
FacebookApplicationID +
"&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup");

help with tweetsharp API v2 for WP7

I'm using the new version of tweetsharp api (v2) and i've had some problems with implementation on wp7...
i'm developping one app that use this api for tweet phrases to user account who's use my application...
so to configure the twiter user account i save the login and password to... when user wants to tweet i get access to his account and tweet that phrase...
My problem is how to make the login to twitter account... i try this but it's not woking....
private void button1_Click(object sender, RoutedEventArgs e)
{
var service = new TwitterService(consumerKey, consumerSecret);
Action<OAuthAccessToken, TwitterResponse> act = new Action<OAuthAccessToken, TwitterResponse>((a, b) => Result(a, b));
try
{
service.GetAccessTokenWithXAuth(username,password,act);
}
catch (Exception) {
}
}
private void Result(OAuthAccessToken a, TwitterResponse b)
{
}
I've read the Api v2 documentation but it's diferent than my method because some methods are new and different than the documentation reports....
thanks very much for help...
stab- have twitter ok'd you for xAuth? Apparently, it's only permissible if your app/apiKeys have been whitelisted for it.
Read this: https://dev.twitter.com/docs/oauth/xauth
Your other apps for iPhone may have used regular OAuth and not xAuth. Like dethSwatch said, Apps have to be approved by Twitter in order to use xAuth.
Only caveat with xAuth is you do not get access tokens to direct messages.

Categories

Resources