Skip to content

Instantly share code, notes, and snippets.

@vquaiato
Forked from rodolfofadino/HomeController.cs
Created September 19, 2012 03:54
Show Gist options
  • Save vquaiato/3747594 to your computer and use it in GitHub Desktop.
Save vquaiato/3747594 to your computer and use it in GitHub Desktop.
Exemplo Facebook SDK C# (teste)
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Facebook;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
dynamic parameters = new ExpandoObject();
parameters.client_id = "31936308815862";
parameters.redirect_uri = "http://localhost:49275/home/login_success";
parameters.response_type = "code";
parameters.display = "popup";
// add the 'scope' parameter only if we have extendedPermissions.
var extendedPermissions = "user_about_me,read_stream,publish_stream";
if (!string.IsNullOrWhiteSpace(extendedPermissions))
{
// A comma-delimited list of permissions
parameters.scope = extendedPermissions;
}
var _fb = new FacebookClient();
var url = _fb.GetLoginUrl(parameters);
return Redirect(url.ToString());
}
public ActionResult Login_success()
{
var _fb = new FacebookClient();
FacebookOAuthResult oauthResult;
//Pega o Code
if (!_fb.TryParseOAuthCallbackUrl(Request.Url, out oauthResult))
{
//erro
}
if (oauthResult.IsSuccess)
{
//Pega o Access Token "permanente"
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("client_id", "31936308815862");
parameters.Add("redirect_uri", "http://localhost:49275/home/login_success");
parameters.Add("client_secret", "722a67e46823d158b9ac4f01bc6492ccc");
parameters.Add("code",oauthResult.Code);
dynamic result = _fb.Get("/oauth/access_token", parameters);
var accessToken = result.access_token;
//Guardar no banco
//Exemplo de uso
var client = new FacebookClient(accessToken);
dynamic me = client.Get("me");
}
else
{
// user cancelled
}
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment