我正在使用C#开发桌面应用程序。
访问https://www.facebook.com/dialog/oauth?client_id=123后,用户登录并将用户访问令牌附加到重定向uri。没问题,当登录页面显示在我的表单中的webbrowser控件中时,我可以从URL中提取令牌。
但这不是我想获得令牌的方式。我的问题是,有没有办法通过Graph API调用获取新创建的令牌?
因为我想在用户的标准浏览器中显示登录页面而不是在此嵌入式Web浏览器中。我获取用户访问令牌的所有努力都导致获取 app 访问令牌,在这种情况下这是无用的。
任何提示都表示赞赏。
答案 0 :(得分:2)
//这是非常原始的,注意它是一个MVC3解决方案,但它在C#中,我希望它有所帮助。 //它基本上是FB上用于'Server Side Flow'的PHP示例的C#版本 //我已经有一段时间了,不得不经历一些痛苦 //请注意我读到的有关这两个//请求的redirect_uri必须相同的错误 //还读到如果redirect_uri没有以'/'结尾,则有人有问题 //如果你有任何问题,请回复,因为我刚刚开始这个项目,我将尝试//合并C#FaceBook SDK
public class AccountController : Controller
{
// LoginWithFaceBook
// First Contact with FB - oauth?client_id ... redirect_uri = /Account/FacebookLinker
// according to a bug files on FB redirect_uri MUST BE SAME FOR both trips ( to get the 'code' then exchange the code for 'access_token'
public ActionResult ConnectFaceBookAccount()
{
string APP_ID = HttpContext.Application["FacebookAppId"].ToString();
string redirect_uri = HttpContext.Application["FacebookOAuthRedirect"].ToString();
string state = HttpContext.Application["state_guid"].ToString();
// in this View I simply link to this URL
ViewBag.FaceBookOAuthUrl = "https://www.facebook.com/dialog/oauth?client_id=" + APP_ID + "&redirect_uri="+redirect_uri+"&state=" + state+"&display=popup";
return View();
}
// Account/FacebookLinker
// redirect_uri for both getting 'code' and exchanging for 'access_token'
public ActionResult FacebookLinker()
{
if (!Request.IsAuthenticated)
{
Response.Redirect("/Account/LogOn");
}
// Per FB DOC, Make sure 'state' var returned is same one you sent to reduce chance of Cross Site Forgery
if (Request.QueryString["state"].ToString() == HttpContext.Application["state_guid"].ToString())
{
try
{
string FBcode = Request.QueryString["code"].ToString();
string APP_ID = HttpContext.Application["FacebookAppId"].ToString();
string APP_SECRET = HttpContext.Application["FacebookAppSecret"].ToString();
string redirect_uri = HttpContext.Application["FacebookOAuthRedirect"].ToString();
string FBAccessUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + APP_ID + "&redirect_uri=" + redirect_uri + "&client_secret=" + APP_SECRET + "&code=" + FBcode;
string accessToken = null;
// Send the request to exchange the code for access_token
var accessTokenRequest = System.Net.HttpWebRequest.Create(FBAccessUrl);
HttpWebResponse response = (HttpWebResponse) accessTokenRequest.GetResponse();
// handle response from FB
// this will not be a url with params like the first request to get the 'code'
Encoding rEncoding = Encoding.GetEncoding(response.CharacterSet);
using(StreamReader sr = new StreamReader(response.GetResponseStream(),rEncoding))
{
// parse the response to get the value of the 'access_token'
accessToken = HttpUtility.ParseQueryString(sr.ReadToEnd()).Get("access_token");
}
//TODO
// Add to the accessToken for the Logged in User.Identity to a FBUSERS Model
// WHen someone Logs in Check to see if they are also in FB
// ON Login Page add option to login with FaceBook
return View();
}
catch (Exception exp)
{
// try to get token failed
}
}
else
{
// state var form FB did not match state var sent
}
return View();
}
答案 1 :(得分:0)
我认为这可以通过URL协议处理程序实现;
myfbapp://accesstoken/{token}
)redirect_uri
设置为您的脸谱页