Monday, May 23, 2011

Upload Video to Facebook in C#

HI Folks,
If you tried to upload a video to facebook using facebook sdk in C#, You will face a problem that the provided method UploadVideo in LegacyRestApi.cs file accepts the full physical path of the file to be uploaded. For ASP.NET developers and since we are using AsyncFileUpload Control to select your files, this control provides only the file name and not the full physical path because of security constraints applied on all browsers. I have updated the source code and ask the codeplex product facebook sdk team to include this in the future release. and I'd like to share the updated code for this method in my blog for all facebook .NET Developers, Hope this helps.

C# Code:

public static string UploadVideo(string accessToken,string fileName, byte[] fileBytes)
 {          
   var mediaObject = new FacebookMediaObject 
    {    
       FileName = fileName,     
       ContentType = "video/3gpp" 
    };     
    mediaObject.SetValue(fileBytes);         
    try {         
              var fb = new FacebookClient(accessToken);     
              var parameters = new Dictionary<string, object>();   
              parameters["method"] = "video.upload";       
              parameters["caption"] = "video upload using rest api";   
              parameters["source"] = mediaObject;          
              var result = (IDictionary<string, object>)fb.Post(parameters);   
              var videoId = (string)result["vid"];        
              Console.WriteLine("Video Id: {0}", videoId);   
              // Note: This json result is not the orginal json string as returned by Facebook.    
              Console.WriteLine("Json: {0}", result.ToString());     
              return videoId;      
       }           
       catch (FacebookApiException ex) {     
            // Note: make sure to handle this exception.       
          throw;           
      }        
 }    
Codeplex discussion entry:


Regards,
Mostafa Arafatwitter.com/mostafaelzoghbi

Tuesday, May 17, 2011

Fix It: Sys.WebForms.PageRequestManagerParserErrorException when using update Panel in Facebook Apps

Hi All,

I was developing a Facebook application using Facebook C# SDK and i have an update panel in my page i'd like it to update a grid in it based on selected index changed event for a drop down list.

The problem is that i have the update panel was throwing an exception when i was trying to change the selected index of a drop down list, the error is :
Sys.WebForms.PageRequestManagerParserErrorException when using update Panel

This error is vague, to know exactly what is the problem of the update panel, Install Fiddler which is the best tool for any web developer should have to inspect http requests/responses.

After installing and open Fiddler to inspect all http round trips on my development machine, I found the actual error that was returned from the server is:
Given URL is not allowed by the Application configuration.
So the solution for this problem is a missing or incorrect configuration in your Facebook application. Open the configuration of your Facebook application and select website tab and set the website url for your local Facebook application is running:
Site URL : http://localhost/

Tips:
1) If you set the port number in the site url this won't fix the problem, Just set the localhost without any ports and this will fix the update panel problem is not updating because of the javascript error.
2) You can have SelectedIndexChanged event as AsyncPostBackTrigger in the update
panel and no issues with this because i read some posts were saying it has to be SyncPostBack
which is not actually right.
Hope this helps.


Regards,
Mostafa Arafa
twitter.com/mostafaelzoghbi

Monday, May 16, 2011

Automatic HTTPS redirect in Windows 2008 R2

Hi All,

I was trying to setup automatic redirect from HTTP to HTTPS for my hosted web application in IIS 7.0/7.5 Windows 2008 R2. I spent good time trying to find out a working solution for this problem.

If you tried a solution with custom error pages 403 in IIS to redirect the http to https, this won't work in windows 2008 R2 IIS 7.0/7.5 server, as stated here:


Please follow these steps to have a working solution:
1) Install URL Rewrite 2.0 on your webserver, download url:

2) After installing url rewrite module in IIS, Open IIS Manager and select your website, you will find a newly added icon "Url Rewirte"

3) Make sure to install SSL certificate on your site and site has https binding on port 443.

4) on SSL settings in IIS, un check Require SSL check - IMPORTANT, because this will overwrite the settings you will configure on URL Rewrite and will throw http 403.4 error.

5) Don't add any custom error pages for 403 as mentioned on some blog posts, if you have please remove it.

6) double click on URL rewrite and click on add rule.

7) Select Blank rule and click on ok.

8) On Name field give name to the rule: Redirect to HTTPS

9) Set the following values on the form:
- Requested URL: Select Matches the pattern
- Using: Regular Expression
- Pattern: (.*)
- Under Conditions, Click on Add and type:
Condition Input: {HTTPS}
Check if input string: Matches the pattern
Pattern: ^OFF$
- Under Action section:
Action Type: select Redirect
Redirect URL: https://{HTTP_HOST}{REQUEST_URI}
If you tried this url: https://{HTTP_HOST}/{R:1} as stated in some blogs, this won't work as well.
Redirect Type: See Other (303)

And then click on Apply.

From IIS Manager, Try to browse the application using HTTP, The IIS will direct you to HTTPS automatically.


Hope this helps.

Resources:



Regards,
Mostafa Arafa
twitter.com/mostafaelzoghbi

Tuesday, May 10, 2011

How to install NuGet Facebook Samples Package in VS 2010

Hi All,

I was trying to develop a functionality to upload a video on facebook, To achieve this, you have to include Facebook NuGet Package in your project.

The question was for me ? What's NuGet ? What's the concept of a Package ? Is it different than VS 2010 Extensions ? and how to add this to my project?

After reading few blogs and experiment this by myself, I wrote this blog post to clarify these points to any developer who hear these terms for the first time as it was for me.

What's NuGet ?
It is a VS 2010 Extension to manage third party and open source libraries in your VS 2010 project, it simplifies the process of installing/uninstalling packages in your projects without worrying about the dependencies dlls and associated files to each packages and so after uninstalling any of these packages on your project there is no clutter.

How to install it NuGet Package Manager:

Once you install this, Please restart your VS 2010.
Once you re-open your VS 2010, You will notice new menu items under tools called: Library Package Manager.
This is how you can install/uninstall packages in your solution with command line window called "Package Manager Console".

In my case, I was trying to install Facebook Sample Package in my project, How would i do that ?

Here is the steps that you need to follow:

1) Open your VS 2o1o as administrator, if you didn't do that, you might get an error when connecting to your TFS as i got Access denied.
2) Open your project and set the startup project you would like to add Facebook samples package to it. It is recommended if this is the first time, Open a new empty project and try these steps so you know exactly the added files when you install any package.
3) To explore NuGet Gallery Packages, here is the website:
For Facebook Package, here is the command you should type it in "Package manager Console":

PM> install-package facebook.sample













Now the facebook sample package has been installed successfully and you can see this in the package manager console.

Open your solution and you will find Samples Folder has been added and Facebook folder underneath it.

You can explore the code and use any of these functions in your project since all dlls and files required have been added to you.

To upload a video to facebook, Check LegacyRestApi.CS in your project and you will find a function called "Upload Video".

Enjoy and have fun with Facebook C# Development.

Let me know if you have any questions following these steps.


Regards,
Mostafa Arafa
twitter.com/mostafaelzoghbi

Monday, May 09, 2011

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

Hi All,

If you are experiencing a problem connecting to your SQL Server 2008 R2 DB server from any remote machine in your network, Please make sure of the following:

1) You are able to ping the machine from the remote server.
2) Try to connect make sure that the SQL Server Engine service is up and running, You can open "SQL Server Configuration Manager" and make sure that the service is up and running.
3) Make sure that you have at least one protocol TCP/IP,Named Piper or Shared Memory is allowed on both your client and your DB Server and this you can check it from SQL Configuration Manager.
4) THIS FIXED MY PROBLEM: Disable the Domain firewall in the DB machine that blocks all inbound connections to your DB Server. To do this:
a) From Server Manager.
b) Under Configuration - > Select Windows Firewall.
c) Click on Windows Firewall Properties and disable your domain profile firewall.

Try to refresh your page and you will be able to connect to your DB.

Hope this helps.

Regards,
Mostafa Arafa
twitter.com/mostafaelzoghbi

Unrecognized attribute 'targetFramework' 4.0

Hi Folks,

If you tried to deploy your asp.net application on Win 08 - IIS7 and you were getting this error:
Unrecognized attribute 'targetFramework' 4.0

This means the application pool that runs your website is not configured to run under .NET Framework 4.0.

On IIS 7:
1) Click on your application pool.
2) Click on the right pane, Advanced settings.
3) Under General Tab, Select .NET Framework Version.
4) Select v 4.0.
5) Recycle your application pool by right click on it.
6) Refresh your page.

Refresh your page and it works :)

Hope this helps...


Regards,
Mostafa Arafatwitter.com/mostafaelzoghbi