gpt4 book ai didi

java - Spring Security 和 Facebook OAuth 2.0 与图形 API 集成

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:50:57 25 4
gpt4 key购买 nike

请至少伪造(但来自工作环境而不是“也许这应该工作”)应用程序上下文和 Controller /过滤器,它们将验证和/或自动注册 Facebook 用户。

此链接:http://blog.kadirpekel.com/2009/11/09/facebook-connect-integration-with-spring-security/不行。实际上,我会给任何将其作为答案发布的人减分。我花了 2 个小时在这件事上,但我没有让它工作。经过这次努力,我比平时更大胆,也更愚蠢了:-(

我真的很想看看OAuth 2.0 Facebook 连接的解决方案。并将 Facebook JavaScript API 的使用限制在绝对最低限度。

以下链接显示了我需要的内容: http://www.richardnichols.net/2010/06/implementing-facebook-oauth-2-0-authentication-in-java/

请仅发布此问题的代码。我已经得到了所有我能处理的建议。

更新

如果有人感兴趣,我有 servlet 解决方案并在此处发布答案: Facebook Connect example in JSP (tomcat)

最佳答案

这是 facebook OAuth 2.0 的 MVC 实现该代码在 C# 中,希望它与 java 的相似性可以帮助您。

Controller(Entry point):Controller(在MVC中)是代码中有人点击登录链接后控件到达的点。

 public ActionResult Authenticate()
{
var oauthFacebook = new FacebookOAuth();
if (Request["code"] == null)
{
//Redirect the user to Facebook for authorization.
Response.Redirect(oauthFacebook.AuthorizationLinkGet());
}
else
{
//Get the access token and secret.
oauthFacebook.AccessTokenGet(Request["code"]);
if (oauthFacebook.Token.Length > 0)
{
//We can now make our api calls
var user = oauthFacebook.GetAttributes();
}
}
}

FacebookOAuth 类

public class FacebookOAuth : Oauth
{
public FacebookOAuth()
{
Authorize = "https://graph.facebook.com/oauth/authorize";
AccessToken = "https://graph.facebook.com/oauth/access_token";
CallbackUrl = "http://<YourURLHere>/Authenticate";
AttributesBaseUrl = "https://graph.facebook.com/me/?access_token=";
ConsumerKey = ConfigurationManager.AppSettings["FacebookConsumerKey"];//Ur Consumer Key goes here
ConsumerSecret = ConfigurationManager.AppSettings["FacebookConsumerSecret"];//Ur Consumer secret goes here
Provider = "Facebook";
}

public override string AuthorizationLinkGet()
{
return
string.Format(
"{0}?client_id={1}&redirect_uri={2}&scope=email,user_education_history,user_location,user_hometown",
Authorize, ConsumerKey, CallbackUrl);
}

public User GetAttributes()
{
string attributesUrl = string.Format("{0}{1}", AttributesBaseUrl, Token);
string attributes = WebRequest(Method.Get, attributesUrl, String.Empty);
var FacebookUser = new JavaScriptSerializer().Deserialize<FacebookUser>(attributes);
return new User()
{
FirstName = FacebookUser.first_name,
MiddleName = FacebookUser.middle_name,
LastName = FacebookUser.last_name,
Locale = FacebookUser.locale,
UserEmail = FacebookUser.email,
AuthProvider = Provider,
AuthToken=Token
};
}
}

OAuth 基类(FacebookOAuth 的派生类)

  public abstract class Oauth
{
#region Method enum

public enum Method
{
Get,
Post,
Delete
} ;

#endregion

protected string AccessToken;
protected string AttributesBaseUrl;
protected string Authorize;
protected string CallbackUrl;
protected string ConsumerKey;
protected string ConsumerSecret;
public string Provider { get; protected set; }

public string Token { get; set; }

public virtual string AuthorizationLinkGet()
{
return
string.Format(
"{0}?client_id={1}&redirect_uri={2}&scope=publish_stream,email,user_education_history,user_location",
Authorize, ConsumerKey, CallbackUrl);
}

public void AccessTokenGet(string authToken)
{
Token = authToken;
string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
AccessToken, ConsumerKey, CallbackUrl, 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)
{
Token = qs["access_token"];
}
}
}

public string WebRequest(Method method, string url, string postData)
{
StreamWriter requestWriter;
string responseData = string.Empty;

var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
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);
}

finally
{
requestWriter.Close();
}
}
responseData = WebResponseGet(webRequest);
}
return responseData;
}

public string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData;
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
finally
{
if (webRequest != null) webRequest.GetResponse().GetResponseStream().Close();
if (responseReader != null) responseReader.Close();
}
return responseData;
}
}

关于java - Spring Security 和 Facebook OAuth 2.0 与图形 API 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4650433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com