Facebook's OAuth "Given URL is not permitted by the Application configuration" - c#

I was working on this program that connects my users through their Facebook accounts. It's been working quiet stable for the past couple of weeks, and I was able to add more value to my program during that period. However, starting today I was unable to connect through Facebook as I usually do. Whenever I ask for App authorization (1st step) I get a "Given URL is not permitted by the Application configuration" error, Even tho i didn't change anything in my App Settings.
I looked everywhere to check if maybe Facebook did some Updates or something, but couldn't find anything. Last time i logged in successfully was the 30th of March.
How could have this happened if I didn't edit any of my code, and apparently Facebook didn't either! Any help would be greatly appreciated :)
private string GenerateLoginUrlFacebook()
{
dynamic parameters = new ExpandoObject();
parameters.client_id = Globals.FBapplicationID;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
parameters.response_type = "token";
parameters.display = "popup";
if (!string.IsNullOrWhiteSpace(Globals.FBextendedPermissionsNeeded))
parameters.scope = Globals.FBextendedPermissionsNeeded;
var fb = new FacebookClient();
Uri loginUri = fb.GetLoginUrl(parameters);
return loginUri.AbsoluteUri;
}

Related

Did Facebook change completely change their SDK?

I've had a working application that connects and registers my users through their Facebook Accounts and retrieves some public information. Anyway, the application stopped working with Facebook with the new update (V2.6). I haven't changed any of my code, and now it's failing at creating a login URL for Facebook.
Last time I managed to Login successfully was on the 10th of April. I've read the Changelog for V2.6 but couldn't find anything that would/should change the Login flow. Here is the function that used to generate the Login URL and I would just get asked if I'd allow "my app" to access my Facebook information, I'd press allow and everything would continue perfectly. Now, I get the "SECURITY WARNING: Please treat the URL above as you would your password and do not share it with anyone. See the Facebook Help Centre for more information" error when the generated Login URL is called...
private string GenerateLoginUrlFacebook()
{
dynamic parameters = new ExpandoObject();
parameters.client_id = Globals.FBapplicationID;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
parameters.response_type = "token";
parameters.display = "popup";
if (!string.IsNullOrWhiteSpace(Globals.FBextendedPermissionsNeeded))
parameters.scope = Globals.FBextendedPermissionsNeeded;
var fb = new FacebookClient();
Uri loginUri = fb.GetLoginUrl(parameters);
return loginUri.AbsoluteUri;
//return #"https://www.facebook.com/dialog/oauth?client_id=471150563090234&redirect_uri=https://www.facebook.com/connect/login_success.html";
}
Any help would be greatly appreciated!

tweetsharp - app stops being able to tweet after a few hours

I have a asp.net 4.5 webforms site that allows users to link their account to twitter and tweet directly from my site.
My app is registered with twitter and I am able to successfully authorise my app for the user's account and initially can tweet fine, but after a few hours the tweets stop working. I am using tweetsharp to handle the authorisation.
my code is:
TwitterClientInfo twitterClientInfo = new TwitterClientInfo();
twitterClientInfo.ConsumerKey = ConsumerKey;
twitterClientInfo.ConsumerSecret = ConsumerSecret;
var requestToken = new OAuthRequestToken { Token = oauthtoken };
TwitterService twitterService = new TwitterService(ConsumerKey, ConsumerSecret);
OAuthAccessToken accessToken = twitterService.GetAccessToken(requestToken, oauthverifier);
twitterService.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
TwitterUser user = twitterService.VerifyCredentials(new VerifyCredentialsOptions());
SendTweetOptions options = new SendTweetOptions();
options.Status = tweetText;
twitterService.SendTweet(options);
what i have noticed is that while the app is successfully tweeting, the accessToken.Token value that is being used to authenticate the user has a proper value (a long string of numbers and upper/lowercase characters) however when it stops tweeting the accessToken.Token value is just a single question mark "?".
Twitter says it doesn't expire tokens so i am at a loss to understand what is happening or how it can be resolved? if i went in to my twitter account and deauthorised my app and went through the authorisation again it would work fine for a few hours, but obviously that's not something i can ask my users to do.
can anyone suggest a resolution to this - either to stop the accessToken value becoming ? or to handle it and get a proper value if it does (without reauthorising the app)
Well, without beginning to understand the actual issue, I managed to fix it
Instead of retrieving the access token every time via:
var requestToken = new OAuthRequestToken { Token = oauthtoken };
OAuthAccessToken accessToken = twitterService.GetAccessToken(requestToken, oauthverifier);
twitterService.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
i only do that once and store accessToken.Token and accessToken.TokenSecret in the database and retrieve them when tweeting and supply them
twitterService.AuthenticateWith(accessTokenFromDB, accessokenSecretFromDB);
I have seen somewhere that Twitter doesn't expire tokens, so this should work. Certainly it's been working for me all weekend whereas the original code would stop working after a few hours.
Thought this might help some others who have the same issue.

Facebook multi account

I want to write a simple win-forms tool for managing different social accounts.I have a problem if user has several Facebook accounts. When I try to log in as second user I just got redirecting page in webBrowser.I tried to use InternetSetOption and the INTERNET_SUPPRESS_COOKIE_PERSIST flag but it seems it does not help. How can I resolve this problem?
Login code
var lParameters = new Dictionary<string, object>();
lParameters["client_id"] = AppId;
lParameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
lParameters["response_type"] = "token";
lParameters["display"] = "popup";
lParameters["scope"] = "user_about_me";
Uri lUri = mFacebookClient.GetLoginUrl(lParameters);
Main.webBrowser.Navigate(lUri);
ADDED:
Maybe I'm doing something wrong with InternetSetOption? Sorry for newbie question. I really should use it like in this answer How do I use InternetSetOption? It looks difficult...
Most probable cause is because Facebook is setting a cookie with the session ID. INTERNET_SUPPRESS_COOKIE_PERSIST only makes cookies non-persistent, they will be cleared after the browser is destroyed or browser session finished, so if you are using the same instance they will still be alive.
You can finish your browsing session with InternetSetOption(0, 42, NULL, 0); (taken from here: http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/76a38eee-3ef6-4993-a54d-3fecc4eb6cff), so combining INTERNET_SUPPRESS_COOKIE_PERSIST and INTERNET_OPTION_END_BROWSER_SESSION (is what the 42 means) it should be cleared and ready for a new login.

Facebook C# SDK - Post to wall

I'm developing an asp.net MVC 3 Facebook app and I am trying to post a message to my wall. Here is my code:
FacebookWebClient client = new FacebookWebClient();
// Post to user's wall
var postparameters = new Dictionary<string, object>();
postparameters["message"] = "Hello world!";
postparameters["name"] = "This is a name";
postparameters["link"] = "http://thisisalink.com;
postparameters["description"] = "This is a description";
var result = client.Post("/me/feed", postparameters);
I can get the access token using client.AccessToken, so I'm assuming I don't have to set it anywhere. This code produces no errors and for the result I get an ID. However, when I bring up my Facebook, I see nothing on my wall nor in my news feed. I'm not sure what I'm missing. I've looked at related questions here at StackOverflow, but I see no reports/questions similar to mine. I've also tried changing the code based on what I've seen in other posts, but to no avail. I also checked my Facebook account settings and I see my application listed with permission to post to my wall. I also tried posting a message to my wall via the Graph API explorer and I'm getting the same result. I get an ID in return, but when I check my Facebook account I see nothing. At been at this for a couple of days. Any help would be greatly appreciated.
Thanks in advance.
[EDIT]
I wonder if something is wrong with my app generated access_token. Using this access_token, as I mentioned in my post, I get the same result using the Graph API explorer. An ID is returned, but no message on my wall. However, if I give the Graph API explorer permission to post to my wall and use its own generated access_token, I can successfully post a message using the explorer. Here's the FB login button code:
<div>
<h1>Login using Facebook</h1>
<p><fb:login-button perms="user_location, publish_stream, email"></fb:login-button></p>
</div>
Basically you need to add an additional parameter into your post parameters.
args["access_token"] = account.access_token;
this is the token of the specific page.
I wont repeat the code, follow here for example: Post On Facebook Page As Page Not As Admin User Using Facebook C# SDK
(second answer)
Did you request right App permissions to Facebook in your action method?
Try: [CanvasAuthorize(Permissions = "user_location, publish_stream, email")]
Did you append the access_token to the URL? See example here. It's also documented here (see Using the Access Token).
/me/feed?access_token=<access_token>
with one small change, your code works fine for me. you have to pass the users auth token into the constructor like this...
var client = new FacebookClient(accessToken);
// Post to user's wall
var postparameters = new Dictionary<string, object>();
postparameters["message"] = "Hello world!";
postparameters["name"] = "This is a name";
postparameters["link"] = "http://thisisalink.com;
postparameters["description"] = "This is a description";
var result = client.Post("/me/feed", postparameters);
this method works for me, although i am not sure which facebook sdk you are using.
i'm using http://facebooksdk.net/ its quite good so if you're not using it i would recommend it

DotNetOpenAuth with Google Calendar Feed

I have been racking my brain for a few days trying to get a list of calendars from Google using DotNetOpenAuth.
I can successfully get a list of contacts using the DotNetOpenAuth Samples. I have integrated it with my domain using the OpenId+OAuth. Everything works great to get a list of contacts.
So from there I modified the code to try to retrieve a list of Calendars and I keep getting a 401 Unauthorized error.
I know it is authorizing because I can get the contact list. Does anyone have a code example how they are retrieving calendars or calendar events using the DotNetOpenAuth with Google???
Thanks
Update:
Thanks for the response. I have read everything I can get my hands on. Here is what I have done so far
Step 1: I created a new GetCalendarEndPoint in the GoogleConsumer.cs
private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("https://www.google.com/calendar/feeds/default", HttpDeliveryMethods.GetRequest);
Step 2: Next I created a new method GetCalendars patterned after the GetContacts Method in GoogleConsumer.cs - (Rebuilt the dll etc.)
public static XDocument GetCalendars(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/) {
if (consumer == null)
{
throw new ArgumentNullException("consumer");
}
var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken);
var response = consumer.Channel.WebRequestHandler.GetResponse(request);
string body = response.GetResponseReader().ReadToEnd();
XDocument result = XDocument.Parse(body);
return result;
Step 3: In my Application I modified the ScopeURI to the the Calendar URI from GoogleConsumer as follows
private IAuthenticationRequest GetGoogleRequest()
{
Realm realm = Request.Url.Scheme + Uri.SchemeDelimiter + Global.GoogleTokenManager.ConsumerKey + "/";
IAuthenticationRequest authReq = relyingParty.CreateRequest(GoogleOPIdentifier, realm);
// Prepare the OAuth extension
string scope = GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Calendar);
Global.GoogleWebConsumer.AttachAuthorizationRequest(authReq, scope);
// We also want the user's email address
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
authReq.AddExtension(fetch);
return authReq;
}
However, when I run the app I get 401 Unauthorized when I make the following call
var calendars = GoogleConsumer.GetCalendars(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);
I have also checked that the State.GoogleAccess token exists by simply displaying it on my screen before I trigger the method that makes this call.
Again, if I exectute
var calendars = GoogleConsumer.GetContacs(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);
then it works??????? Thanks for you help.
I've been suffering through exactly the same thing for most of the weekend.
I think that after much fiddling with Fiddler I've found the cause and have a solution which, although not pretty, seems to work. I found that I was able to access the calendar feed by copying and pasting the DNOA-generated Uri into a browser, but always got a 401 when attempting programmatic access. This is apparently because the default auto-redirect behavior of HttpWebRequest discards any cookies that the redirect is attempting to set. The Contacts feed doesn't set any cookies during the redirect, so it is immune.
The first time you request a calendar feed (even with a properly constructed and signed OAuth request), Google replies with a redirect containing a cookie. If you don't present that "calendar cookie" at the same time as your feed request you will get a 401 Unauthorized when you attempt to follow the redirect to the feed.
here's the cookie-setting header from Google:
HTTP/1.1 302 Moved Temporarily
Set-Cookie: S=calendar=y7AlfgbmcqYl0ugrF-Zt9A;Expires=Tue, 10-Jan-2012 03:54:20 GMT;Secure
Here's what I'm doing to make it work:
// wc: WebConsumer
var calRequest = wc.PrepareAuthorizedRequest(erp2, authTokenRsp.AccessToken);
// need to stop redirect to capture calendar cookie token:
calRequest.AllowAutoRedirect = false;
var calResponse = calRequest.GetResponse();
var redirectCookie = calResponse.Headers[System.Net.HttpResponseHeader.SetCookie];
var cookiedCalRequest = wc.PrepareAuthorizedRequest(erp2, authTokenRsp.AccessToken);
cookiedCalRequest.Headers[System.Net.HttpRequestHeader.Cookie] = redirectCookie;
var calFeedResponse = cookiedCalRequest.GetResponse();
Have you read the Google Calendar data API documentation to make sure you have the right endpoints programmed in? Have you also modified the code that acquires the access token to request access to Google Calendar in addition to Google Contacts? The access token in the sample only gets Contacts permissions unless you change it.

Categories