1

我正在开发一个 Windows Phone 应用程序,我必须在其中将照片发布到 facebook。并且通过使用 PhotoChooserTask 或 CameraChooserTask 选择特定的照片。

通常,我可以成功发布特定照片,但发布所选照片时遇到问题。我看到了一些链接,如 链接

所以,如果有人知道这个问题,请帮助我。提前谢谢。

编辑

private void PostClicked(object sender, RoutedEventArgs e)
    {
        //Client Parameters
        var parameters = new Dictionary<string, object>();
        //var parameters1 = new Dictionary<>();
        parameters["client_id"] = FBApi;
        parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
        parameters["response_type"] = "token";
        parameters["display"] = "touch";
        parameters["ContentType"] = "image/png";
        //The scope is what give us the access to the users data, in this case
        //we just want to publish on his wall
        parameters["scope"] = "publish_stream";
        Browser.Visibility = System.Windows.Visibility.Visible;
        Browser.Navigate(client.GetLoginUrl(parameters));
    }
private void BrowserNavitaged(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult oauthResult;
        //Making sure that the url actually has the access token
        if (!client.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
        {
            return;
        }
        //Checking that the user successfully accepted our app, otherwise just show the error
        if (oauthResult.IsSuccess)
        {
            //Process result
            client.AccessToken = oauthResult.AccessToken;
            //Hide the browser
            Browser.Visibility = System.Windows.Visibility.Collapsed;
            PostToWall();
        }
        else
        {
            //Process Error
            MessageBox.Show(oauthResult.ErrorDescription);
            Browser.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
private void PostToWall()
    {
        string imageName = "ic_launcher.png";
        StreamResourceInfo sri = null;
        Uri jpegUri = new Uri(imageName, UriKind.Relative);
        sri = Application.GetResourceStream(jpegUri);
        try
        {
            byte[] imageData = new byte[sri.Stream.Length];
            sri.Stream.Read(imageData, 0, System.Convert.ToInt32(sri.Stream.Length));
            FacebookMediaObject fbUpload = new FacebookMediaObject
            {
                FileName = imageName,
                ContentType = "image/jpg"
            };
            fbUpload.SetValue(imageData);
            string name1 = eventname.Text;
            string format = "yyyy-MM-dd";
            string message1 = eventmessage.Text;
            string date1 = datepicker.ValueString;
            DateTime datevalue = DateTime.Parse(date1);
            string d = datevalue.ToString(format);
            string memoType = "Tribute";
            var parameters = new Dictionary<string, object>();
            var parameters1 = new Dictionary<string, object>();
            parameters["message"] = name1 + "\n" + d + "\n" + memoType + "\n" + message1;
            parameters["source"] = fbUpload;

            webservice();
            client.PostTaskAsync("me/photos", parameters);
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }

        //client.PostTaskAsync("me/photos", parameters1);
    }

单击按钮时,我正在调用 PostClicked 类,它将直接转到 facebook 主页并询问登录信息。我正在这样做。请检查一下

4

2 回答 2

2

现在我可以使用 photochoosertask 或 cameratask 成功地将照片分享到 Facebook。我正在分享我的经验,以便如果有人遇到同样的问题可以使用它。

private void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        BitmapImage image = new BitmapImage();
        image.SetSource(e.ChosenPhoto);
        SaveImageToIsolatedStorage(image, tempJPEG);
        this.image.Source = image;
    }
public void SaveImageToIsolatedStorage(BitmapImage image, string fileName)
    {
        using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isolatedStorage.FileExists(fileName))
                isolatedStorage.DeleteFile(fileName);
            var fileStream = isolatedStorage.CreateFile(fileName);
            if (image != null)
            {
                var wb = new WriteableBitmap(image);
                wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
            }
            fileStream.Close();
        }
    }

有了这个,您可以将选定的图像保存到 IsolatedStorage。然后在将照片发布到 Facebook 时,您必须从独立存储中选择图像。

private void PostClicked(object sender, RoutedEventArgs e)
    {
        //Client Parameters
        var parameters = new Dictionary<string, object>();
        parameters["client_id"] = FBApi;
        parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
        parameters["response_type"] = "token";
        parameters["display"] = "touch";
        //The scope is what give us the access to the users data, in this case
        //we just want to publish on his wall
        parameters["scope"] = "publish_stream";
        Browser.Visibility = System.Windows.Visibility.Visible;
        Browser.Navigate(client.GetLoginUrl(parameters));
    }
private void BrowserNavitaged(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult oauthResult;
        //Making sure that the url actually has the access token
        if (!client.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
        {
            return;
        }
        //Checking that the user successfully accepted our app, otherwise just show the error
        if (oauthResult.IsSuccess)
        {
            //Process result
            client.AccessToken = oauthResult.AccessToken;
            //Hide the browser
            Browser.Visibility = System.Windows.Visibility.Collapsed;
            PostToWall();
        }
        else
        {
            //Process Error
            MessageBox.Show(oauthResult.ErrorDescription);
            Browser.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
private void PostToWall()
    {
        try
        {
            byte[] data;
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read))
                {
                    data = new byte[fileStream.Length];
                    fileStream.Read(data, 0, data.Length);
                    fileStream.Close();
                }
            }
            //MemoryStream ms = new MemoryStream(data);
            //BitmapImage bi = new BitmapImage();
            //// Set bitmap source to memory stream 
            //bi.SetSource(ms);
            //this.imageTribute.Source = bi;
            FacebookMediaObject fbUpload = new FacebookMediaObject
            {
                FileName = tempJPEG,
                ContentType = "image/jpg"
            };
            fbUpload.SetValue(data);
            string name1 = eventname.Text;
            string format = "yyyy-MM-dd";
            string message1 = eventmessage.Text;
            string date1 = datepicker.ValueString;
            DateTime datevalue = DateTime.Parse(date1);
            string d = datevalue.ToString(format);
            string memoType = "Notice";
            var parameters = new Dictionary<string, object>();
            var parameters1 = new Dictionary<string, object>();
            parameters["message"] = name1;
            parameters["source"] = fbUpload;
            webservice();
            client.PostTaskAsync("me/photos", parameters);

        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }
    }

谢谢大家....

于 2013-08-27T06:36:58.077 回答
0

你可以通过两种方法做到这一点:

1)通过使用mediasharetask,它将向您显示您的手机同步到的所有共享帐户,如facebook、gmail、linkdin、twitter等:它可以像这样使用。

           ShareMediaTask shareMediaTask = new ShareMediaTask();
           shareMediaTask.FilePath = path;
           shareMediaTask.Show();

2)通过使用facebook sdk。您可以从 nuget manager 获取包,然后您可以使用它在 facebook 上分享。

我希望这可以帮助你。

于 2013-08-26T11:18:13.600 回答