Facebook C# SDK for Facebook Connect - facebook

How can I use Facebook C# SDK for facebook connect using ASP.NET?
What I am trying to do is:
Provide a "Login with Facebook" button.
Once the user clicks on Login button, get the access token on server side and sign in to my website.
Most articles (I found through google or SO) said use Facebook Javascript SDK for Log in and Authentication and then get the Access token from cookies created on web browser.
What I did so far:
Used Facebook login button in .aspx page with the below code.
<fb:login-button onlogin="window.location.reload();" perms="offline_access">Login with Facebook</fb:login-button>
Problems I am having:
When I click log in button on MY PAGE a window will pop up to log in to facebook. MY PAGE is supposed to reload when I login to facebook but it reloads even I click on Cancel button or close the pop up window. What I need is reloading MY PAGE only when I log in.
From the above step: If I log in and MY PAGE reloads perfectly. In code behind (Page load event) I can see the cookie created and it has the information (access token) what I needed. And from here I want to use Facebook C# SDK to get user information. how to do ?
I used Facebook C# SDK 3 months ago for developing a canvas application in Facebook. The documentation and examples listed on Codeplex are for Apps on Facebook (correct me if I missed to see facebook connect examples). Also, there was a link to documentation which lists all the Facebook C# SDK classes but I could not see that now. Please provide me the link if you know.
Any help will be greatly appreciated.
Thank you.

http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-First-Facebook-Application-v6.aspx
But there is one issue I am not using
parameters.response_type = "token";
Instead of that i have function to update Token
public static void UpdateToken()
{
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = AppId,
client_secret = AppSecret,
redirect_uri = Url,
code = FacebookUtils.FacebookData.Code
});
FacebookData.AccessToken = result.access_token;
}

Related

Logout from Instgram in my website

I have integrated login with Instagram feature in my website using provided API successfully.
Now i have implement the feature of logout from Instagram and then website should redirect to home page of website. But I am not getting a way to do this, please help me in this matter. I have tried lots of solutions given by other people.
First : Destroy all session variables of website on logout page to logout from your website.
Second: Use below JavaScript code to logout from Instagram
var s = document.createElement("script");
s.src = "https://instagram.com/accounts/logout";
$("head").append(s);

Google Chrome Extension + Login with Facebook + Parse

I'm trying to build a google chrome extension, a use case requires that users can login with Facebook and share via a post to their Facebook wall.
The correct application flow would be as such:
User click google chrome extension - summons extension page
User clicks "login with facebook" on the extension page (or in a new tab) the users then see the "approve app" from Facebook
The user approves the app
The user is directed to a new page that thanks them for installing and processes all the needed user information OR the user is sent back to the google chrome extension (better way)
If the user is not sent back to the google chrome extension, they should be told to do so, but at this point how to get the data to the google chrome extension?
The user opens to the Google chrome extension, they should now see "Hi John" or similar instead of "Login with Facebook"
The user can no post to Facebook
My major hick-ups are logging in with Facebook, and getting the data back to the extension.
I've created a Facebook app, done most of the coding specific to the app, I'm trying to keep the app just javascript (but I can add in php if there is no other way), tried using the standard Facebook javascript, and tried using the desktop client for Facebook.
Anyone else gone through this process of connecting Google Chrome Extensions and Facebook login before?
As a final note, I'm trying to with with Parse for this project, so if you have any extra insite on that it would be great too!
A good example of what I'm trying to accomplish is similar to what Any.do
https://chrome.google.com/webstore/search/any.do?utm_source=chrome-ntp-icon
I had this kind of problem lately ;). It's a bit complicated, but I'll try to explain it in the simplest way.
Put somewhere in your extension (maybe options page) link "login with facebook" that redirects to: https://www.facebook.com/dialog/oauth?client_id=<APP_ID>&response_type=token&scope=<PERMISSIONS>&redirect_uri=http://www.facebook.com/connect/login_success.html
When someone clicks on this link, will be asked to give permissions, etc (facebook page). If someone would agree, the page will load: http://www.facebook.com/connect/login_success.html with parameters access_token and expires_in in address.
Now, background page should have script like this:
var successURL = 'www.facebook.com/connect/login_success.html';
function onFacebookLogin(){
if (!localStorage.getItem('accessToken')) {
chrome.tabs.query({}, function(tabs) { // get all tabs from every window
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].url.indexOf(successURL) !== -1) {
// below you get string like this: access_token=...&expires_in=...
var params = tabs[i].url.split('#')[1];
// in my extension I have used mootools method: parseQueryString. The following code is just an example ;)
var accessToken = params.split('&')[0];
accessToken = accessToken.split('=')[1];
localStorage.setItem('accessToken', accessToken);
chrome.tabs.remove(tabs[i].id);
}
}
});
}
}
chrome.tabs.onUpdated.addListener(onFacebookLogin);
And this is how you get access token from facebook. Now you can send requests to https://graph.facebook.com.
Hope I helped ;)
To answer your question (Don't have enough credit to comment).
In your Facebook developer settings, click the advanced tab at the top, make sure "Client OAuth login" & "Embedded browser OAuth login" are both moved to "Yes" and then I had to add "http://www.facebook.com/" to the Valid OAuth redirect URI's
now facebook has changed "Client Oauth Login" button location. You need to add a product and after adding facebook login you will found here

Can't figure out how to get access token using facebook c# sdk

I'm currently writing a Facebook app that runs in a page tab. I'm writing it as a simple Web Forms app in C#, using the latest version of the C# SDK pulled from NuGet. The main page tab works fine. I get all the info I need from FacebookWebContext.Current.SignedRequest, and I'm fine there. I'm trying to write a page now that the page admin would use to set up the app, so this is the page that would go under the "Edit URL" in the app setup.
All I really want to do is get the currently signed-on user's ID, and determine if he's an admin for the page in question.
I'm doing this:
var client = new FacebookClient();
dynamic me = client.Get("me");
in Page_Load, and getting the following exception:
(OAuthException) An active access token must be used to query information about the current user.
I've tried a bunch of stuff to get the access token, but I don't seem to know what I'm doing wrong. Is there a straightforward way of doing this?
Update:
I figured out at one point that I had a reference to the old JS SDK on my master page, and that was causing a problem. (See here). Removing that got me to the point where I was usually able to see whether or not the user was actually logged in to Facebook.
Try this:
var fbContext = FacebookWebContext.Current;
if (fbContext.IsAuthenticated())
{
var client = new FacebookClient(fbContext.AccessToken);
dynamic me = client.Get("me");
}
As far as I understand, being logged-in in Facebook does not mean FacebookWebcontext.Current.isAuthenticated() == true. What Facebook wants to make sure is that the user has authorized the app. So what you should do is to forward the page to facebook authorization dialogue page (https://www.facebook.com/dialog/oauth?) with necessary permissions. Facebook will determine whether the app has been authorized by the user and redirect to the redirect_uri with a newer access_token issued.

Facebook Check if user "Liked" the page

I made a few Facebook FBML Canvas pages (Facebook considers these to be Applications) for a company, and they requested that users who "Like" the page would be redirected to another page, or the page will display different content with AJAX.
I went through the Facebook Developers Documentation, but I can't find a clue on how to do it.
I'm sure someone has done it before, any suggestions?
I found this on the Facebook forum I think it might help you out!
How can i redirect when someone like/share?
I haven't tried it myself though!
Good luck!
You can use either the JS or PHP sdk to check if a user liked a page. See examples below...
Uses JS sdk to check if a user liked a page (can be any page, just set the page id). This can be used in a standalone app or from within a page tab. - http://unbound-media.com/facebook/how-to-tell-if-a-facebook-user-likes-your-facebook-application/
Uses PHP sdk to check if a user liked the page from a tabbed app (can only check if user liked the current page they're on). This method can also be converted to JS. - How to check if a user likes my Facebook Page or URL using Facebook's API
If you are dealing with facebook app tab page, the following code helps you to find isfan status
$signed = parse_signed_request($_REQUEST['signed_request'], 'YOUR-APP-SECRET');
if ($signed['page']['liked'] == 1) {
$fan = true;
} else {
$fan = false;
}
Visit this link for demo and source code.

Facebook canvas Iframe App authentication problem

I am in the conversion process of facebook app from fbml to iframe.
Now i am testing using few iframe pages.
$user = $facebook->require_login();
It gives the current user logged in. Once it get the variable from the facebook.com it saves in cookie by the PHP API provided by facebook).
I logged out using another tab of facebook and i tryed using my app without refreshing the whole site (by just using the links inside my app). It still gives the $user variable.
It indicates that user has logged in instead of user logged out.
Please help me out. I want my app secured.
It looks my iframe app accessible when we select "open this frame in new window".
I need a solution for this too.
Thanks in advance.
You are logged in as far as you are logged in facebook. You will have to create custom PHP's session handling. However, if you want to remove the user from application's cookies, you can do this:
//this will clear cookies for your application and redirect them to a login prompt
$facebook->set_user(null, null);
$facebook->redirect($appcallbackurl);
You can use the JavaScript SDK to subscribe to the logout event via FB.Event.subscribe and reload the page if that happens:
FB.Event.subscribe('auth.logout', function(response) {
setTimeout('window.location.reload()', 0);
});
(Wrapping in setTimeout() is required as a work-around for Firefox.)
To prevent users from using "open this frame in new window," you can check if the page is loaded in an iframe with if(window == window.top) or if the signed_request is available. If not, redirect to the tab URL.