上传图片 - Facebook C#

时间:2016-08-11 14:54:01

标签: c# winforms facebook-graph-api

我必须使用C#在Facebook上发布一些图片。 我有一个用户可以连接到Facebook的视图,以及另一个允许用户共享照片的视图。我认为我的代码一切正常,但仍然无法正常工作......任何人都可以帮助我吗? 这是代码:

Facebook连接:

    //button connection
    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate(GenerateLoginUrl(AppId, "publish_actions"));
        webBrowser1.Show();
    }

    private Uri GenerateLoginUrl(string appId, string extendedPermissions)
    {
        dynamic parameters = new ExpandoObject();
        parameters.client_id = appId;
        parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
        parameters.response_type = "token";

        parameters.display = "popup";

        if (!string.IsNullOrWhiteSpace(extendedPermissions))
            parameters.scope = extendedPermissions;
        var fb = new FacebookClient();
        return fb.GetLoginUrl(parameters);
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var fb = new FacebookClient();
        FacebookOAuthResult oauthResult;
        if (fb.TryParseOAuthCallbackUrl(e.Url, out oauthResult))
        {
            if (oauthResult.IsSuccess)
            {
                var accesstoken = oauthResult.AccessToken;
                token = accesstoken.ToString();
                Kinomaton.Properties.Settings.Default.token = token;
                Kinomaton.Properties.Settings.Default.Save();
            }
        }
    }

上传图片按钮

    //uplaod button
    private void button2_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate("Http://www.google.fr");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var official = new FacebookClient(Kinomaton.Properties.Settings.Default.token);
        dynamic parameters = new ExpandoObject();
        parameters.message = "Message";
        parameters.picture = @""; //path of my picture
        parameters.privacy = new
        {
            value = "ALL_FRIENDS",
        };
        dynamic result = official.Post("me/feed", parameters);
    }

    public static string GenerateFacebookSecretProof(string content, string key)
    {
        byte[] keyBytes = Encoding.UTF8.GetBytes(key);
        byte[] messageBytes = Encoding.UTF8.GetBytes(content);
        byte[] hash;
        using (HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes))
        {
            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();
    }

0 个答案:

没有答案