Sho*_*gdm 10 facebook facebook-graph-api facebook-authentication asp.net-mvc-4 facebook-apps
我注意到2017年3月27日Facebook GRAPH API到期后,我的Facebook应用程序无法提供对我的Web应用程序的访问.主要OAuthWebSecurity是无法从facebook图形API获取登录状态,我已经完成了开发人员组中的错误报告.但他们没有提供解决方案.我得到了一个解决方案,NopCommerce在此链接中提供.但是我没有得到任何完美的解决方案MVC 4.
我已经通过了这个链接,他们的系统在接近MVC 5,但是我需要在解决MVC4这一点,提OWIN不支持MVC4在VS 2012
那么,如何解决这个问题.自从过去一周以来,我一直在经历这个问题.
主要是这段代码中发生异常
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
Run Code Online (Sandbox Code Playgroud)
它返回IsSuccessful为false,其余参数也为null.
我研究称Facebook已经改变了API的返回类型string,以JSON因此,如何发展自己的代码来获取那些JSON完全吻合.谢谢.
更新: 此问题仍未得到答复.有人在这里帮助我.
你检查过这个解决方案吗?
"我们在2017年3月27日星期一遇到同样的问题,当时Facebook停止支持他们的Graph API v2.2.我们也使用DotNetOpenAuth,最初是通过Nuget安装的.源代码可以在下面的链接中找到"
FacebookApplication.VerifyAuthentication(_httpContext,GenerateLocalCallbackUri())在Facebook上返回null
编辑(2017年4月18日):您好,我今天早上遇到了以下链接,它为我解决了这个问题.试试让我知道:)
https://github.com/DotNetOpenAuth/DotNetOpenAuth/issues/397
代码由用户'adoconnection'提供.这是链接中的第4个帖子.
我把头发拉过来.大声笑
最好的祝福!
根据之前的回答,我必须给你一个解决方案。每个人都MVC4写下他们的AppID和SecurityCode。由于 facebook GRAPH API 的更改,以前的链接已损坏。因此每个人都需要改变RegisterFacebookClient职业。但该类是.Net 库中的密封类,因此任何人都无法扩展或覆盖它。因此我们需要使用一个wrapper类。让我们考虑我的包装类,FacebookClientV2Dot3因此我的类将是
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using DotNetOpenAuth.AspNet.Clients;
using Newtonsoft.Json;
public class FacebookClientV2Dot3 : OAuth2Client
{
#region Constants and Fields
/// <summary>
/// The authorization endpoint.
/// </summary>
private const string AuthorizationEndpoint = "https://www.facebook.com/dialog/oauth";
/// <summary>
/// The token endpoint.
/// </summary>
private const string TokenEndpoint = "https://graph.facebook.com/oauth/access_token";
/// <summary>
/// The user info endpoint.
/// </summary>
private const string UserInfoEndpoint = "https://graph.facebook.com/me";
/// <summary>
/// The app id.
/// </summary>
private readonly string _appId;
/// <summary>
/// The app secret.
/// </summary>
private readonly string _appSecret;
/// <summary>
/// The requested scopes.
/// </summary>
private readonly string[] _requestedScopes;
#endregion
/// <summary>
/// Creates a new Facebook OAuth2 client, requesting the default "email" scope.
/// </summary>
/// <param name="appId">The Facebook App Id</param>
/// <param name="appSecret">The Facebook App Secret</param>
public FacebookClient(string appId, string appSecret)
: this(appId, appSecret, new[] { "email" }) { }
/// <summary>
/// Creates a new Facebook OAuth2 client.
/// </summary>
/// <param name="appId">The Facebook App Id</param>
/// <param name="appSecret">The Facebook App Secret</param>
/// <param name="requestedScopes">One or more requested scopes, passed without the base URI.</param>
public FacebookClient(string appId, string appSecret, params string[] requestedScopes)
: base("facebook")
{
if (string.IsNullOrWhiteSpace(appId))
throw new ArgumentNullException("appId");
if (string.IsNullOrWhiteSpace(appSecret))
throw new ArgumentNullException("appSecret");
if (requestedScopes == null)
throw new ArgumentNullException("requestedScopes");
if (requestedScopes.Length == 0)
throw new ArgumentException("One or more scopes must be requested.", "requestedScopes");
_appId = appId;
_appSecret = appSecret;
_requestedScopes = requestedScopes;
}
protected override Uri GetServiceLoginUrl(Uri returnUrl)
{
var state = string.IsNullOrEmpty(returnUrl.Query) ? string.Empty : returnUrl.Query.Substring(1);
return BuildUri(AuthorizationEndpoint, new NameValueCollection
{
{ "client_id", _appId },
{ "scope", string.Join(" ", _requestedScopes) },
{ "redirect_uri", returnUrl.GetLeftPart(UriPartial.Path) },
{ "state", state },
});
}
protected override IDictionary<string, string> GetUserData(string accessToken)
{
var uri = BuildUri(UserInfoEndpoint, new NameValueCollection { { "access_token", accessToken } });
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
using (var webResponse = webRequest.GetResponse())
using (var stream = webResponse.GetResponseStream())
{
if (stream == null)
return null;
using (var textReader = new StreamReader(stream))
{
var json = textReader.ReadToEnd();
var extraData = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
var data = extraData.ToDictionary(x => x.Key, x => x.Value.ToString());
data.Add("picture", string.Format("https://graph.facebook.com/{0}/picture", data["id"]));
return data;
}
}
}
protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
{
var uri = BuildUri(TokenEndpoint, new NameValueCollection
{
{ "code", authorizationCode },
{ "client_id", _appId },
{ "client_secret", _appSecret },
{ "redirect_uri", returnUrl.GetLeftPart(UriPartial.Path) },
});
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
string accessToken = null;
HttpWebResponse response = (HttpWebResponse)webRequest.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))
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = serializer.DeserializeObject(sr.ReadToEnd());
var jConvert = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(jsonObject));
Dictionary<string, object> desirializedJsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(jConvert.ToString());
accessToken = desirializedJsonObject["access_token"].ToString();
}
return accessToken;
}
private static Uri BuildUri(string baseUri, NameValueCollection queryParameters)
{
var keyValuePairs = queryParameters.AllKeys.Select(k => HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(queryParameters[k]));
var qs = String.Join("&", keyValuePairs);
var builder = new UriBuilder(baseUri) { Query = qs };
return builder.Uri;
}
/// <summary>
/// Facebook works best when return data be packed into a "state" parameter.
/// This should be called before verifying the request, so that the url is rewritten to support this.
/// </summary>
public static void RewriteRequest()
{
var ctx = HttpContext.Current;
var stateString = HttpUtility.UrlDecode(ctx.Request.QueryString["state"]);
if (stateString == null || !stateString.Contains("__provider__=facebook"))
return;
var q = HttpUtility.ParseQueryString(stateString);
q.Add(ctx.Request.QueryString);
q.Remove("state");
ctx.RewritePath(ctx.Request.Path + "?" + q);
}
}
Run Code Online (Sandbox Code Playgroud)
看这里,我已将所有 API 链接替换为较新版本的链接。
现在你需要修改你的
认证配置
只需使用包装类
OAuthWebSecurity.RegisterClient(new FacebookClientV2Dot3("AppID", "HassedPassword"));
Run Code Online (Sandbox Code Playgroud)
然后一切成功。您的 Facebook 登录将恢复到之前的状态。
然而,您可能会面临关于这个新 API 而不是以前的 API 的新问题,问题是IP Whitelisting. 就像这个图像。希望除了这个你什么都不需要。快乐编码。
| 归档时间: |
|
| 查看次数: |
1226 次 |
| 最近记录: |