如何发送appsecret_proof使用Facebook c#SDK? [英] How to send appsecret_proof using facebook c# SDK?

查看:244
本文介绍了如何发送appsecret_proof使用Facebook c#SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Facebook应用程序上使用需要应用程序秘密(需要应用程序秘密服务器API调用)
但是如果我这样做 - 我收到以下错误:


(GraphMethodException - #100)没有指定appsecret_proof参数



描述:执行期间发生未处理的异常的当前Web请求。请查看堆栈跟踪以获取有关错误的更多信息及其在代码中的位置。



异常详细信息:Facebook.FacebookApiException:(GraphMethodException - #100)没有指定appsecret_proof参数



源错误:



行801:var fb = new FacebookClient(accessToken);
行802:
行803:动态FacebookInfo = fb.Get(/ me?appsecret_proof =+ fb.AppSecret +& fields = email,birthday,gender); / strong>
第804行:signInInfo.Email = facebookInfo.email;
第805行:


我看到这篇文章,所以我试图理解如何发送它...我需要切换到fb.Post ?



另外,我想知道SDK是否还没有像GenereateFaceBookSecret()



谢谢提前。

解决方案

SOLVED!最后...和新的Facebook API v2.4



所以也许我可以保存别人6个小时: - )



我创建了这个小帮手类:



 命名空间YouProjectNamespace.Helpers {using System.Security.Cryptography;使用System.Text; ///< summary> /// Facebook Helper ///< / summary> public static class FacebookHelper {///< summary> ///生成Facebook秘密证明(与Facebook API v2.4一起使用)///< seealso cref =http://stackoverflow.com/questions/20572523/c-sharp-help-required-to-create- Facebook的appsecret防-hmacsha256\" /> ///< / summary> ///< param name =facebookAccessToken>< / param> ///< param name =facebookAuthAppSecret>< / param> ///< returns>< / returns> public static string GenerateFacebookSecretProof(string facebookAccessToken,string facebookAuthAppSecret){byte [] keyBytes = Encoding.UTF8.GetBytes(facebookAuthAppSecret); byte [] messageBytes = Encoding.UTF8.GetBytes(facebookAccessToken); HMACSHA256 hmacsha256 =新型HMACSHA256(keyBytes); byte [] hash = hmacsha256.ComputeHash(messageBytes); StringBuilder sbHash = new StringBuilder(); for(int i = 0; i< hash.Length; i ++){sbHash.Append(hash [i] .ToString(x2)); } return sbHash.ToString(); }}}  



这是如何使用它的: p>

  //使用Facebook SDK for .NET获取更具体的数据(https://github.com/facebook-csharp-sdk/facebook-csharp -sdk)var identityApplicationProvider(); //用户名/密码(*) facebookAccessToken,facebookAuthAppSecret);动态的FacebookInfo = fb.Get(string.Format(/ me?appsecret_proof = {0}& fields = email,birthday,gender,facebookAppSecretProof)); signInInfo.Email = facebookInfo.email; code> 



我应该补充说可以添加为了使用Facebook SDK,
这是我在Startup.Auth.cs



  #region Facebook / / https://developers.facebook.com/apps // https://developers.facebook.com/docs/facebook-login/permissions/v2.4 // https://developers.facebook.com/docs/graph -api / reference / v2.4 / post // https://developers.facebook.com/docs/apps/changelog#v2_4 // https://developers.facebook.com/docs/graph-api/reference/user var facebookAuthOptions = new FacebookAuthenticationOptions(); facebookAuthOptions.AppId = facebookAuthAppId; facebookAuthOptions.AppSecret = facebookAuthAppSecret; facebookAuthOptions.SendAppSecretProof = true; // public_profile(默认)包括:id,name,first_name,last_name,age_range,link,gender,locale,timezone,updated_time,verified facebookAuthOptions.Scope.Add(public_profile); facebookAuthOptions.Scope.Add( 电子邮件); facebookAuthOptions.Scope.Add( user_birthday); facebookAuthOptions.Scope.Add( USER_LOCATION); //当前城市通过用户对象上的位置字段facebookAuthOptions.Provider = new FacebookAuthenticationProvider(){OnAuthenticated =(context)=> {// http://stackoverflow.com/questions/7999934/facebook-c-sharp-sdk-problems-getting-user-email/8013211#8013211 // http://blogs.msdn.com/b/webdev/ archive-2013/10/16 / get-more-information-from-social-providers-used-in-the-vs-2013-project-templates.aspx //从FB获取访问令牌并将其存储在数据库中,使用FacebookC#SDK获取有关用户的更多信息context.Identity.AddClaim(new System.Security.Claims.Claim(FacebookAccessToken,context.AccessToken)); var expiryDuration = context.ExpiresIn?新TimeSpan(); context.Identity.AddClaim(new Claim(facebook:expires_in,DateTime.UtcNow.Add(expiryDuration).ToString(CultureInfo.InvariantCulture))); //添加所有其他可用的声明foreach(在上下文中的变量声明.User){var claimType = string.Format(facebook:{0},claim.Key); var claimsValue = claim.Value.ToString(); if(!context.Identity.HasClaim(claimType,claimValue))context.Identity.AddClaim(new System.Security.Claims.Claim(claimType,claimValue,XmlSchemaString,Facebook)); } return Task.FromResult(0); }}; app.UseFacebookAuthentication(facebookAuthOptions); #endregion Facebook  


I want to use "Require App Secret" (Require app secret for server API calls) on my facebook app, But if I do - I'm getting the following error:

(GraphMethodException - #100) No appsecret_proof parameter was specified

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Facebook.FacebookApiException: (GraphMethodException - #100) No appsecret_proof parameter was specified

Source Error:

Line 801: var fb = new FacebookClient(accessToken); Line 802: Line 803: dynamic facebookInfo = fb.Get("/me?appsecret_proof=" + fb.AppSecret + "&fields=email,birthday,gender"); Line 804: signInInfo.Email = facebookInfo.email; Line 805:

I saw this post, so I'm trying to understand how to send it... do I need to switch to fb.Post ?

Also, I wonder if the SDK does not already have something like "GenereateFaceBookSecret()"

Thanks in advance.

解决方案

SOLVED! finally... and working with the new facebook APIs v2.4

So maybe I can save someone else 6 hours :-)

I created this little helper class:

namespace YouProjectNamespace.Helpers 
{
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// Facebook Helper
    /// </summary>
    public static class FacebookHelper
    {
        /// <summary>
        /// Generate a facebook secret proof (works with facebook APIs v2.4)
        /// <seealso cref="http://stackoverflow.com/questions/20572523/c-sharp-help-required-to-create-facebook-appsecret-proof-hmacsha256"/>
        /// </summary>
        /// <param name="facebookAccessToken"></param>
        /// <param name="facebookAuthAppSecret"></param>
        /// <returns></returns>
        public static string GenerateFacebookSecretProof(string facebookAccessToken, string facebookAuthAppSecret)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(facebookAuthAppSecret);
            byte[] messageBytes = Encoding.UTF8.GetBytes(facebookAccessToken);
            HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes);
            byte[] hash = hmacsha256.ComputeHash(messageBytes);
            StringBuilder sbHash = new StringBuilder();
            
            for (int i = 0; i < hash.Length; i++)
            {
                sbHash.Append(hash[i].ToString("x2"));
            }

            return sbHash.ToString();
        }
    }
}

And this is how to use it:

// Use Facebook SDK for .NET to get more specific data (https://github.com/facebook-csharp-sdk/facebook-csharp-sdk)

var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
var facebookAccessToken = identity.FindFirstValue("FacebookAccessToken");
var fb = new FacebookClient(facebookAccessToken);

var facebookAuthAppSecret = "Use_Your_Own_Facebook_AppSecret_Here";
var facebookAppSecretProof = FacebookHelper.GenerateFacebookSecretProof(facebookAccessToken, facebookAuthAppSecret);

dynamic facebookInfo = fb.Get(string.Format("/me?appsecret_proof={0}&fields=email,birthday,gender", facebookAppSecretProof));
signInInfo.Email = facebookInfo.email;

I should add that a claim should be added in order to use facebook SDK, This is what I have in Startup.Auth.cs

            #region Facebook

            // https://developers.facebook.com/apps
            // https://developers.facebook.com/docs/facebook-login/permissions/v2.4
            // https://developers.facebook.com/docs/graph-api/reference/v2.4/post
            // https://developers.facebook.com/docs/apps/changelog#v2_4
            // https://developers.facebook.com/docs/graph-api/reference/user

            var facebookAuthOptions = new FacebookAuthenticationOptions();

            facebookAuthOptions.AppId = facebookAuthAppId;
            facebookAuthOptions.AppSecret = facebookAuthAppSecret;
            facebookAuthOptions.SendAppSecretProof = true;

            // public_profile (Default) includes: id,name,first_name,last_name,age_range,link,gender,locale,timezone,updated_time,verified
            facebookAuthOptions.Scope.Add("public_profile");
            facebookAuthOptions.Scope.Add("email");
            facebookAuthOptions.Scope.Add("user_birthday");
            facebookAuthOptions.Scope.Add("user_location"); // current city through the location field on the User object

            facebookAuthOptions.Provider = new FacebookAuthenticationProvider()
            {
                OnAuthenticated = (context) =>
                {
                    // http://stackoverflow.com/questions/7999934/facebook-c-sharp-sdk-problems-getting-user-email/8013211#8013211
                    // http://blogs.msdn.com/b/webdev/archive/2013/10/16/get-more-information-from-social-providers-used-in-the-vs-2013-project-templates.aspx
                    // Get the access token from FB and store it in the database and use FacebookC# SDK to get more information about the user
                    context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));

                    var expiryDuration = context.ExpiresIn ?? new TimeSpan();
                    context.Identity.AddClaim(new Claim("facebook:expires_in", DateTime.UtcNow.Add(expiryDuration).ToString(CultureInfo.InvariantCulture)));

                    // Add all other available claims
                    foreach (var claim in context.User)
                    {
                        var claimType = string.Format("facebook:{0}", claim.Key);
                        var claimValue = claim.Value.ToString();
                        if (!context.Identity.HasClaim(claimType, claimValue))
                            context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
                    }

                    return Task.FromResult(0);
                }
            };
            app.UseFacebookAuthentication(facebookAuthOptions);

            #endregion Facebook

这篇关于如何发送appsecret_proof使用Facebook c#SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆