Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Created November 27, 2011 00:58
Show Gist options
  • Save rodolfofadino/1396673 to your computer and use it in GitHub Desktop.
Save rodolfofadino/1396673 to your computer and use it in GitHub Desktop.
AuthFacebook .NET C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Configuration;
public class AuthFacebook
{
public enum Method { GET, POST };
public const string AUTHORIZE = "https://graph.facebook.com/oauth/authorize";
public const string ACCESS_TOKEN = "https://graph.facebook.com/oauth/access_token";
public string CALLBACK_URL = ConfigurationManager.AppSettings["Facebook_callback"].ToString();
private string _consumerKey = "";
private string _consumerSecret = "";
private string _token = "";
#region Properties
public string ConsumerKey
{
get
{
if (_consumerKey.Length == 0)
{
_consumerKey = ConfigurationManager.AppSettings["Facebook_consumerKey"]; //application ID
}
return _consumerKey;
}
set { _consumerKey = value; }
}
public string ConsumerSecret
{
get
{
if (_consumerSecret.Length == 0)
{
_consumerSecret = ConfigurationManager.AppSettings["Facebook_consumerSecret"]; //application secret
}
return _consumerSecret;
}
set { _consumerSecret = value; }
}
public string Token { get { return _token; } set { _token = value; } }
#endregion
public string AuthorizationLinkGet()
{
return string.Format("{0}?client_id={1}&redirect_uri={2}&scope={3}", AUTHORIZE, this.ConsumerKey, CALLBACK_URL, "publish_stream");
}
public string AuthorizationLinkGetPopup()
{
return string.Format("{0}?client_id={1}&display=popup&redirect_uri={2}&scope={3}", AUTHORIZE, this.ConsumerKey, CALLBACK_URL, "publish_stream");
}
public void AccessTokenGet(string authToken)
{
this.Token = authToken;
string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
ACCESS_TOKEN, this.ConsumerKey, CALLBACK_URL, this.ConsumerSecret, authToken);
string response = WebRequest(Method.GET, accessTokenUrl, String.Empty);
if (response.Length > 0)
{
//Store the returned access_token
NameValueCollection qs = HttpUtility.ParseQueryString(response);
if (qs["access_token"] != null)
{
this.Token = qs["access_token"];
}
}
}
public string WebRequest(Method method, string url, string postData)
{
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = "";
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
webRequest.UserAgent = "[You user agent]";
webRequest.Timeout = 20000;
if (method == Method.POST)
{
webRequest.ContentType = "application/x-www-form-urlencoded";
//POST the data.
requestWriter = new StreamWriter(webRequest.GetRequestStream());
try
{
requestWriter.Write(postData);
}
catch
{
throw;
}
finally
{
requestWriter.Close();
requestWriter = null;
}
}
responseData = WebResponseGet(webRequest);
webRequest = null;
return responseData;
}
public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = "";
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch
{
throw;
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}
}
@rodolfofadino
Copy link
Author

Gist para Autenticar e Trabalhar com a API do Facebook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment