(using server-side flow) i have a facebook application that is attached to a facebook page through one of the tabs, which makes the application within an iframe. i use the following code below which works to get the access token but only when the users click on the facebook logo and authenticate the app. my question is what should i do so i can get a better user experience instead of showing the facebook logo stuff.
thanks.
ON pageLoad()......
protected void Page_Load(object sender, EventArgs e)
{
string code = Request.QueryString["code"];
if (HttpContext.Current.Session["fbAccessToken"] != null)
{
authToken = HttpContext.Current.Session["fbAccessToken"].ToString();
}
if (!String.IsNullOrEmpty(authToken))
{
thisUserFBID.Value = HttpContext.Current.Session["fbUserId"].ToString();
}
else if (code == "" || code == null)
{
Response.Redirect(appHelp.GetCodeUrl());
//Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "shareMe();", true);
}
else
{
authToken = appHelp.GetAccessToken(code);
Session["access_token"] = authToken;
HttpContext.Current.Session["fbAccessToken"] = authToken;
try
{
var fb = new FacebookClient();
fb.AccessToken = authToken;
dynamic me = fb.Get("me");
thisUserFBID.Value = (string)me.id;
HttpContext.Current.Session["fbUserId"] = thisUserFBID.Value;
if (me.locale != null)
lang = (string)me.locale;
else
lang = "en_US";
HttpContext.Current.Session["lang"] = lang;
}
catch (Exception)
{
//Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "shareMe();", true);
//Response.Redirect(appHelp.GetCodeUrl());
}
}
}
Huh? You don't want to have the user know what they're logging into? I'm confused. In a page tab application, you can either prompt them for authorizing the app or if they're logged in already, show them the good stuff. I don't know what would make a better user experience than that.
Related
It seems that nowdays there's no way to allow to post onto facebook user groups via a new, unreviewed app.
I've created an App with live features availible to gen public, with approvements for email, public_profile, user_friends.
I'm trying to post on the wall of a group I've created and I'm an admin of.
Now, when firing up my code, I get the nasty "OAuthException - #200) (#200) Insufficient permission to post to target..." exception.
Posting my wall work perfectly...
public class HomeController : Controller
{
private const long GroupId = 15473890820xxxx;
//
// GET: /Home/
public ActionResult Index(string code)
{
ViewBag.Message = "Welcome to Facebook App Demo!";
//user denied permissions on Facebook.
if (Request["error_reason"] == "user_denied")
{
//this is not implemented. For reference only.
return RedirectToAction("LogOn", "Account");
}
if (string.IsNullOrEmpty(code))
{
ViewBag.Error = "There was an error while loggin into Facebook. Please try again later.";
return RedirectToAction("LogOn", "Account");
}
var fb = new FacebookClient();
dynamic result = fb.Post("oauth/access_token", new
{
client_id = "93199615018xxxx",
client_secret = "2089ae4447877e7388500b844235xxxx",
redirect_uri = "http://localhost:13301/",
code = code
});
var appToken = result.access_token as string;
// update the facebook client with the access token so
// we can make requests on behalf of the user
fb.AccessToken = appToken;
//get extended App/User Token
dynamic result2 = fb.Get("oauth/access_token", new
{
client_id = "93199615018xxxx"
client_secret = "2089ae4447877e7388500b844235xxxx",
grant_type="fb_exchange_token",
fb_exchange_token = appToken,
// code = code
});
var extendedAppToken = result2.access_token as string;
//generate Extended User Token (valid for 60 days)
Session["Facebooktoken"] = extendedAppToken;
dynamic me = fb.Get("me/accounts");
var response = fb.Get("/me/accounts?access_token=" + extendedAppToken) as JsonObject;
return View(me);
}
[HttpPost]
public ActionResult Index(FormCollection collection) //rename to PostWall
{
ViewBag.Message = "Welcome to Facebook App Demo!";
// Post to the wall..
try
{
if (Session["Facebooktoken"] != null)
{
string message = collection["txtPost"].ToString();
FacebookClient client = new FacebookClient(Session["Facebooktoken"].ToString());
dynamic result = client.Post(GroupId+"/feed", new
{
message = message
});
ViewBag.SaveMessage = "Successfuly Post on your wall : " + result.id;
}
else
{
return RedirectToAction("LogOn", "Account");
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
}
return View();
}
public ActionResult About()
{
return View();
}
}
public class AccountController : Controller
{
//
// GET: /Account/
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
var facebookAppId = "93199615018xxxx";
var facebookAppSecret = "2089ae4447877e7388500b844235xxxx";
var facebookRedirectUrl = "http://localhost:13301/";
//
var facebookScope = "email,publish_pages,publish_actions,manage_pages,user_managed_groups,user_hometown,user_website";
if (facebookAppId != null && facebookAppSecret != null && facebookRedirectUrl != null && facebookScope != null)
{
var url = string.Format(#"https://www.facebook.com/dialog/oauth/?client_id={0}&redirect_uri={1}&scope={2}", facebookAppId, facebookRedirectUrl, facebookScope);
Response.Redirect(url, true);
}
return this.RedirectToAction("Index", "Home");
}
}
Any clue on how to set up an testing environment for above functionality without the extra-round of establishing a fully fledged App that has to be reviewed by Facebook admins? I've looked on testing versions for Apps and Users, but no groups either.
Many thanks in advance
Darren, thanks for your considerations. It seems like due to my privacy restrictions I couldnt get any group info of my personal account. Either programmatically, nor manually in Facebooks Graph API Explorer.
It worked finally on an business (fan)'Page' profile by getting an 'long-lived' Page Access Token in facebooks Access Token Debuger.
Yes, user_managed_groups and publish_actions had been enabled.
It was working fine before but It's not working even though I haven't changed anything in my Facebook related code. It is giving this error:
App doesn't give permission to given URL : The settings of app doesn't allow one or more of the given URL's. URLs must be Website's URL or Canvas URL...
Here is my FacebookLoginPage.cs:
namespace MyApp.Pages
{
public partial class FacebookLoginPage : PhoneApplicationPage
{
private string message;
public FacebookLoginPage()
{
InitializeComponent();
message = String.Empty;
this.Loaded += FacebookLoginPage_Loaded;
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.GoBack();
base.OnBackKeyPress(e);
}
private async void FacebookLoginPage_Loaded(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(App.AccessToken))
{
App.isAuthenticated = true;
await Authenticate();
}
}
private FacebookSession session;
private async Task Authenticate()
{
//Facebook logini kontroli eğer login olduysa AccessToken ve bilgileri çeker.
try
{
if (App.FacebookSessionClient.LoginInProgress == true && !String.IsNullOrEmpty(message))
{
App.FacebookSessionClient.LoginInProgress = false;
}
else
{
session = await App.FacebookSessionClient.LoginAsync("user_about_me,read_stream");
App.AccessToken = session.AccessToken;
App.appSettings["accessToken"] = App.AccessToken;
App.appSettings.Save();
App.FacebookId = session.FacebookId;
Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri("/Pages/MainPage.xaml?token=" + App.AccessToken, UriKind.Relative)));
}
}
catch (InvalidOperationException)
{
message = "failed";
App.FacebookSessionClient.LoginInProgress = true;
NavigationService.GoBack();
}
}
}
}
What might be the probelm?
When I added facebook.com to Oauth part of Advanced Setting in my Facebook App, the problem solved. Thanks to the following link: Windows Phone 8 Facebook Login Given URL is not allowed by the application
I am getting FacebookAuthorizationException: User logged in as different Facebook user , while trying to update permission to publish ,My code looks like below
onCreate(...) {
connectFacebookAccount();
}//oncreate
private void connectFacebookAccount() {
settings = getSharedPreferences("PREF_NAME",MODE_PRIVATE);
String FbExperi = settings.getString("FB_AccessExpires, null);
String accesstoken = settings.getString(FB_AccessToken, null);
if (accesstoken != null) {
// open a session from the access token info
// saved in the app's shared preferences
Date date = null;
try {
date = (Date) new SimpleDateFormat("MMMM d, yyyy",
Locale.ENGLISH).parse(FbExperi);
} catch (ParseException e) {
e.printStackTrace();
}
AccessToken accessToken = AccessToken
.createFromExistingAccessToken(accesstoken, date, null,
null, null);
Session.openActiveSessionWithAccessToken(NewTest.this, accessToken,
new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
if (session != null && session.isOpened()) {
Session.setActiveSession(session);
getPublishPremission();
}
}
});
}
}
private void getPublishPremission(){
if (Session.getActiveSession().getPermissions() == null
|| !Session.getActiveSession().getPermissions()
.containsAll(PERMISSIONS)) {
Session.getActiveSession().requestNewPublishPermissions(
new Session.NewPermissionsRequest(
NewTest.this, PERMISSIONS));
}else{
publishFeedDialog()
}
private void publishFeedDialog() {
//open WebDialog to publish feed
}
The execution flow revolves around requestNewPublishPermissions and StatusCallback hence the permission is not set .I request your help to point out where i went wrong.I have an logged in using different facebook Id in my facebook app.In my app i am trying to log-in using different facebook Id from the access token info saved in the app's shared preferences.
see this link
There is a bug in facbook sdk,when there is no facebbok app installed on device.When user ask for additional permission,it takes user to the login dialog again.
I am new to facebook c# sdk. I followed the tutorial in this link.
I created an application that displays the user name after log in. Here is my code:
public partial class MainWindow : Window
{
private string appId = "appid";
private string extenededPermissions = "offline_access,publish_stream";
private Uri loginUrl = null;
private string accessToken = null;
private string userName = null;
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// Function to get the login url
/// with the requested permissions
/// </summary>
private void GetLoginUrl()
{
dynamic parameters = new ExpandoObject();
// add the client id
parameters.client_id = appId;
// add the redirect uri
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
// requested response
parameters.response_type = "token";
// type of display
parameters.display = "popup";
// If extended permissions are present
if (!string.IsNullOrWhiteSpace(extenededPermissions))
parameters.scope = extenededPermissions;
// Create the login url
Facebook fc = new FacebookClient();
loginUrl = fc.GetLoginUrl(parameters);
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// get the login url
GetLoginUrl();
// Navigate to that page
webBrowser.Navigate(loginUrl);
}
private void webBrowser_Navigated(object sender, NavigationEventArgs e)
{
var fc = new FacebookClient();
FacebookOAuthResult fr;
// Check the returned url
if (fc.TryParseOAuthCallbackUrl(e.Uri, out fr))
{
// check if authentication is success or not
if (fr.IsSuccess)
{
getUserName(out userName);
}
else
{
var errorDes = fr.ErrorDescription;
var errorReason = fr.ErrorReason;
}
}
else
{
}
}
private void getUserName(out string name)
{
var fb = new FacebookClient(accessToken);
// Get the user details
dynamic result = fb.Get("me");
// Get the user name
name = result.name;
MessageBox.Show("Hai " + name + ",Welcome to my App");
}
}
My Problem is with the FacebookOAuthResult.
private void webBrowser_Navigated(object sender, NavigationEventArgs e)
{
var fc = new FacebookClient();
FacebookOAuthResult fr;
// Check the returned url
if (fc.TryParseOAuthCallbackUrl(e.Uri, out fr))
{
// check if authentication is success or not
if (fr.IsSuccess)
{
getUserName(out userName);
}
else
{
var errorDes = fr.ErrorDescription;
var errorReason = fr.ErrorReason;
}
}
else
{
}
}
After I logged in it is redirecting to redirect_uri. But the fc.TryParseOAuthCallbackUrl(e.Uri, out fr) fails though the webbrowser redirects to the Authentication successful page.
So I couldn't get the access token. What could the problem in my code be?
This doesn't answer the question, but I see you are asking for an offline_access permission. Facebook removed offline_access sometime ago. Instead you need an Extended Access Token. You get it by exchanging the access token you are trying to get, for an extended one. They last for about 2-3 months after which you have to get a new one.
Nevermind i have found out the solution..Thanks to the answers for the question!
I have added the Winforms web browser control to the wpf and the authentication is working.The problem is with WPF web browser. It simply omits the url after # token So the parseurl won't able to authenticate it.
Here's the modified code..
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// create the windows form host
System.Windows.Forms.Integration.WindowsFormsHost sample =
new System.Windows.Forms.Integration.WindowsFormsHost();
// create a new web browser
webBrowser = new System.Windows.Forms.WebBrowser();
// add it to winforms
sample.Child = webBrowser;
// add it to wpf
canvas1.Children.Add(sample);
webBrowser.Navigated += webBrowser_Navigated;
webBrowser.Navigate(loginURL);
}
void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
// do the authentication
var fc = new FacebookClient();
FacebookOAuthResult fr;
// Check the returned url
if (fc.TryParseOAuthCallbackUrl(e.Url, out fr))
{
// check if authentication is success or not
if (fr.IsSuccess)
{
accessToken = fr.AccessToken;
// Actions to do
}
else
{
var errordes = fr.ErrorDescription;
var errorreason = fr.ErrorReason;
}
}
else
{
//Not a valid url
}
}
The problem is solved!!
I have a problem logging out of Facebook on Windows Phone. I have the code to login, but I can't log out of it. Can someone show me how to log out of Facebook on a Windows Phone?
Here's the login code:
private FacebookClient _asyncFbClient;
private string _appID = "";
private string _appSecret = "";
private void StartFacebookLogin(object sender, RoutedEventArgs e)
{
string[] extendedPermissions = new[] { "user_about_me", "publish_stream", "email" };
FacebookClient fb = new FacebookClient();
var oauth = new FacebookOAuthClient { AppId = _appID, AppSecret = _appSecret };
var logout = new FacebookUser();
var parameters = new Dictionary<string, object>
{
{"response_type", "token"},
{"display", "touch"}
};
if (extendedPermissions != null && extendedPermissions.Length > 0)
{
var scope = new StringBuilder();
scope.Append(string.Join(",", extendedPermissions));
parameters["scope"] = scope.ToString();
}
var loginUrl = oauth.GetLoginUrl(parameters);
webBrowser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(CheckForAuth);
webBrowser.Navigate(loginUrl);
}
private void CheckForAuth(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
FacebookOAuthResult result;
if (FacebookOAuthResult.TryParse(e.Uri, out result))
{
if (result.IsSuccess)
{
IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings;
MessageBox.Show(result.AccessToken);
access = result.AccessToken;
if (Settings.Contains("MyFacebookAccessToken1"))
Settings["MyFacebookAccessToken1"] = result.AccessToken;
else
Settings.Add("MyFacebookAccessToken1", result.AccessToken);
Settings.Save();
_asyncFbClient = new FacebookClient(result.AccessToken);
_asyncFbClient.GetCompleted += new EventHandler<FacebookApiEventArgs>(_asyncFbClient_GetCompleted);
_asyncFbClient.GetAsync("/me");
}
}
}
Here's the logout code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(linkUrl);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(LogoutResponse), request);
private void LogoutResponse(IAsyncResult result)
{
string responseData = "";
try
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
responseData = reader.ReadToEnd();
//responseData = true
}
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(() => { MessageBox.Show(ex.Message); });
}
}
I have tried to use Facebook's logout API and get a successful result, but the next time I open the web browser it's already automatically logged in to Facebook again. How can I get it to log out?
I tried quite a few hacks to get this to work... and then Facebook finally updated their documentation with a good answer:
Logout
You can log a user out of their Facebook session by directing them to
the following URL:
https://www.facebook.com/logout.php?next=YOUR_URL&access_token=ACCESS_TOKEN
YOUR_URL must be a URL in your site domain, as defined in the
Developer App.
From http://developers.facebook.com/docs/authentication/
there is no FB Logout, it has been removed.
your best bet is to forget your credentials.
Facebook PHP SDK uses a function like this:
public function getLogoutUrl($params=array()) {
return $this->getUrl(
'www',
'logout.php',
array_merge(array(
'next' => $this->getCurrentUrl(),
'access_token' => $this->getAccessToken(),
), $params)
);
}
Which creates a URL like:
https://www.facebook.com/logout.php?next={YOUR_ENCODED_URL}&access_token={YOUR_ACCESS_TOKEN}
I believe the encoded URL must be owned by the application to whom the access_token belongs.
If you get that URL right, it'll work (just tried for one of my applications).
I'm not 100% sure if it works on windows-phone, but you can give it a try.