Problems logging into Facebook with C# Winforms - c#

Hi so I'm creating a simple C# Winform application that will let users login to facebook. So far, when I run the program, a message "The parameter app_id is required". In my code below, I have added my app_id but it is still not working. How can I fix this? On the designer portion of the form, I just added the WebBrowser on the form. Nothing too fancy. The code for this form is below. Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Facebook;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Dynamic;
namespace demo
{
public partial class Form1 : Form
{
private const string AppId = "{APP_ID}";
private Uri _loginUrl;
private const string _ExtendedPermissions = "user_about_me,publish_stream,offline_access";
FacebookClient fbClient = new FacebookClient();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Login();
}
private void Login()
{
dynamic parameters = fbClient.Get("oauth/access_token", new
{
client_id = "{APP_ID}",
client_secret = "{SECRET_KEY}",
grant_type = "client_credentials"
});
if (!string.IsNullOrWhiteSpace(_ExtendedPermissions))
parameters.scope = _ExtendedPermissions;
var fb = new FacebookClient();
_loginUrl = fb.GetLoginUrl(parameters);
webBrowserLogin.Navigate(_loginUrl.AbsoluteUri);
}
}
}

Related

Connect C# to mySQL database using windows form

im new to C# and trying to connect a windows form to a mySQL database.
The problem is i get the error below and can't find a way to fix this.
Error CS0120 An object reference is required for the non-static field, method, or property 'Login.con()
This is the Form.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void BtnConnect_Click(object sender, EventArgs e)
{
Login.con();
}
}
}
This is the Login.cs file where the connections should be made.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using MySql.Data;
using System.Data;
namespace Log
{
public class Login
{
public MySqlConnection connection;
private string server;
private string database;
private string user;
private string password;
private string port;
private string connectionString;
private string sslM;
public Login()
{
server = "localhost";
database = "test";
user = "root";
password = "root";
port = "3306";
sslM = "none";
connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);
connection = new MySqlConnection(connectionString);
}
public void con()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
}
}
you need to instance Login class...
Login login = new Login();
login.con()
You have to create a new instance of class Login. Try like:
public void BtnConnect_Click(object sender, EventArgs e)
{
Login login = new Login();
login.con();
}

How to automaticly send an email via outlook to the document controller in case of error

I'm working on a script that shows a form with links to documents.
It's all working correctly but I would like to add a function that automaticly sends an email to the document controller that the selected document is not present.
The email content should contain text (string) with filename of the document that does not exist.
I have found several possibilities but I'm new to C# and I don't know were to start incorporating it in my current script. I want to use Microsoft Outlook for sending the emails.
I have created the following:
#region Namespaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion
using static OpenDocument.Command;
using Form = System.Windows.Forms.Form;
using Application = Autodesk.Revit.ApplicationServices.Application;
using View = Autodesk.Revit.DB.View;
namespace OpenDocument
{
public partial class Form1 : Form
{
#region Variables
//Document links:
string doc01 = #"C:\docs\3D ModelGuide.docx";
string doc02 = #"C:\docs\3D Checklist.docx";
public Form1()
{
InitializeComponent();
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnModelguide3D_Click(object sender, EventArgs e)
{
openFile(doc01);
}
private void btnChecklist3D_Click(object sender, EventArgs e)
{
openFile(doc02);
}
void openFile(string fileName)
{
if (System.IO.File.Exists(fileName) == true)
{
System.Diagnostics.Process.Start(fileName);
}
else
{
TaskDialog.Show("Info", notFoundError(fileName));
}
}
string notFoundError(string fileName)
{
string info = "The requested file/folder" + "\n" + fileName + "\n" + "does not exist!"
return info;
}

CefSharp error: "Browser not yet initialized" when execute "ExecuteScriptAsync" or "EvaluateScriptAsync"

I'm trying to execute javascript using CeSharp.
In the end I got an error:
Browser is not yet initialized. Use the IsBrowserInitializedChanged event and check the IsBrowserInitialized property to determine when the browser has been intialized.
When I used: "ExecuteScriptAsync" method like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace CefSharp
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chromeBrowser;
public Form1()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
private void Form1_Load(object sender, EventArgs e)
{
chromeBrowser.ExecuteScriptAsync("alert('Adam')");
}
public void InitializeChromium()
{
CefSettings settings = new CefSettings
{
CachePath = "cache"
};
// Initialize cef with the provided settings
Cef.Initialize(settings);
// Create a browser component
chromeBrowser = new ChromiumWebBrowser("http://localhost/Test/index.html");
// Add it to the form and fill it to the form window.
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
or when I used "EvaluateScriptAsync"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace CefSharp
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chromeBrowser;
public Form1()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
private void Form1_Load(object sender, EventArgs e)
{
var task = chromeBrowser.EvaluateScriptAsync("alert('Adam')");
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
public void InitializeChromium()
{
CefSettings settings = new CefSettings();
// Initialize cef with the provided settings
Cef.Initialize(settings);
// Create a browser component
chromeBrowser = new ChromiumWebBrowser("http://localhost/Test/index.html");
// Add it to the form and fill it to the form window.
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
Does anyone know how to handle it ?

Problems with namespace

I have to retrieve the SID of a C# software in order to connect it with Facebook.
I saw that I have to print this:
string sid=WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();
But it's written that WebAuthenticationBroker doesn't exist in the context.
So I added using Windows.Security.Authentication.Web; but it still doesn't work.
Do you have some solutions ?
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Facebook;
using System.Net;
using System.Web;
using System.Dynamic;
using Windows;
using Windows.Security.Authentication.Web;
namespace testfb
{
public partial class Form1 : Form
{
private const string AppId = "APPID";
private Uri _loginUrl;
private const string _ExtendedPermissions = "user_about_me, publish_stream, offline_access";
FacebookClient fbClient = new FacebookClient();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string sid = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();
Login();
}
public void Login()
{
dynamic parameters = new ExpandoObject();
parameters.client_id = AppId;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
parameters.response_type = "token";
parameters.display = "popup";
if (!string.IsNullOrWhiteSpace(_ExtendedPermissions))
parameters.scope = _ExtendedPermissions;
var fb = new FacebookClient();
_loginUrl = fb.GetLoginUrl(parameters);
webBrowser2.Navigate(_loginUrl.AbsoluteUri);
}
}
}

How to pass a string to child form?

Basically what I'm trying to do is I have a string on the main form that pulls its value from a textbox.
I then generate a modal version of a second form and want to have that string (or the main forms textbox1.text value) usable in the second form for processes.
Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool{
public partial class MainForm : Form
{
public string hostname;
public MainForm()
{
InitializeComponent();
textBox1.Text = hostname;
}
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.ShowDialog();
}
}
}
'
Child Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool
{
public partial class SiteForm : Form
{
public string hostname {get; set; }
public SiteForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = this.hostname;
}
}
}
Any suggestions on how I can do this? I know there has to be a simpler way, sorry I'm still a bit of a noob and am trying to teach myself C# as I go.
The result is when I click the label on the child form it is blank, because of this I am able to deduce that the string isn't passing between the two forms correctly.
The simplest way is to pass it in the constructor of the Child form, for example:
private string _hostname = "";
...
public SiteForm(string hostname)
{
_hostname = hostname;
InitializeComponent();
}
Try hooking into your child form's Load event and set the value of its hostname property in an event handler on your main form.
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.Load += new EventHandler(frmsite_Load);
frmsite.ShowDialog();
}
public void frmsite_Load(object sender, EventArgs e)
{
SiteForm frmsite = sender as SiteForm;
frmsite.hostname = this.hostname;
}

Categories

Resources