I have a WP7 app using facebook SDK 5.4.1. I try to upload a photo to Facebook, but always get an error message "(OAuthException) (#1) An unknown error occurred".
Here is my code:
var app = new Facebook.FacebookApp(AuthKey);
var parameters = new Dictionary<string, object>
{
{"message","test"},
{"description","test2"}
};
FacebookMediaObject media = new FacebookMediaObject
{
FileName = "1",
ContentType = "image/jpeg"
};
WriteableBitmap wb = new WriteableBitmap(0, 0).FromResource("/Images/thumbnails/Color.png");
media.SetValue(wb.ToByteArray());
parameters.Add("source", media);
app.PostAsync("me/photos", parameters, new FacebookAsyncCallback(postResult));
I can post a message on FB wall using my app, but not for uploading photos(I tried to upload different sizes of photo). What can be wrong? Thanks
You can't use wb.toByteArray() - you need to encode the image to JPEG before uploading it.
Extensions.SaveJpeg() is what you are looking for (here)
Related
I'm getting the error below while trying to upload media to Google Photos API, following the docs
This is how i retrieve my bytes array:
And this is how i make the request:
I've tried a lot of things and none of it work...
Note: I'm consuming other Google Photos API endpoints, such as Get Albums, Create Albums, Get Media and everything work as expected. The upload media is the only one i'm having trouble with.
Note 2: The token is being sent correctly.
Note 3: All the origin endpoints were configured in the google console (localhost included) so much so that other endpoints are working correctly.
Anyone can give me a light?
I am writing something similar in C# and was able to work with the photo api. My best advice is to double check headers. I hope this helps, additionally I added a postman screenshot of a successful call to the api:
public async Task<GoogleApiResponse> UploadPhoto(StorageFile photo)
{
var userInfo = UserInfoVault.GetUserInfo();
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userInfo.AccessToken);
client.DefaultRequestHeaders.Add("X-Goog-Upload-File-Name", photo.DisplayName);
var fileStream = await photo.OpenAsync(FileAccessMode.Read);
var reader = new DataReader(fileStream.GetInputStreamAt(0));
await reader.LoadAsync((uint)fileStream.Size);
byte[] pixels = new byte[fileStream.Size];
reader.ReadBytes(pixels);
var httpContent = new ByteArrayContent(pixels);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage photoResponse = client.PostAsync(ApiEdnpoints.UploadPhoto, httpContent).Result;
string strResponse = await photoResponse.Content.ReadAsStringAsync();
return null;
}
Postman screenshot of successful call to upload a photo
I am trying to upload videos to YouTube but somehow it's not working in Xamarin.Forms (Portable) project. Is there any way to upload the Videos in YouTube through Xamarin.Forms (Portable)
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = Constants.API.Google.ClientID,
ClientSecret = Constants.API.Google.ClientSecret
},
Scopes = new[] { YouTubeService.Scope.Youtube }
};
var flow = new AuthorizationCodeFlow(initializer);
TokenResponse token = flow.LoadTokenAsync("user", CancellationToken.None).Result;
but every time I am getting null in TokenResponse token, I am not sure what should I pass in place of user in flow.LoadTokenAsync("user", CancellationToken.None)
Thank You.
As I had feared the Google .Net client library does not currently support Xamarin. Please see issue: Unable to Google.Apis.Auth.PlatformServices in Xamarin.Forms
It's because GoogleWebAuthorizationBroker requires several classes from Google.Apis.Auth.PlatformServices, and Google.Apis.Auth.PlatformServices is not compatible with Xamarin.
I want to upload an image from the gallery or live pic (camera capture task) with some params; x = "some string", y = "some string", z = "some integer" and uploadimage = name of file tag in form.
Can anyone please help me?
Uploading image for WP8 was pain to setup. Many of the examples I follow was outdated and this took me days to research and finally got one to work. There are a few ways to upload:
1) Convert the image to a string and then you can send the image converted string via HTTP request. I've used this approach for Android, but haven't tried it for WP.
2) Upload the image via FTP and the text data via HTTP.
You have 3 choices to upload any File to server.
Convert file into Stream - recommend way
Convert file into ByteArray
Convert file into String
After that you can use HttpClient package make a POST request to server. Here is the code to demonstrate FileUpload by converting it into Stream.
Code:
public async void methodToUploadFile()
{
StorageFile file = await StorageFile.GetFileFromPathAsync("Assets/MyImage.png");
// var fileBytes = await GetBytesAsync(file);
HttpClient client = new HttpClient();
// give the server URI here
Uri requestUri = new Uri("Full Server URI", UriKind.Absolute);
MultipartFormDataContent formdata = new MultipartFormDataContent();
formdata.Add(new StringContent("some string"), "x");
formdata.Add(new StringContent("some string"), "y");
formdata.Add(new StringContent("some integer"), "z");
formdata.Add(new StreamContent(await file.OpenStreamForReadAsync()), "file", "MyImage.png");
// formdata.Add(new ByteArrayContent(fileBytes), "file", "MyImage.png");
// Make a POST request here
var res = await client.PostAsync(requestUri, formdata);
}
Hope this helps..!
I am trying to create an event using the C# SDK. I am using the code from the following blog:
http://facebooksdk.blogspot.co.uk/2011/05/facebook-create-event.html
And I am using an image that I copied from an existing event on Facebook.
I am getting however the following error:
(OAuthException) (#324) Missing or invalid image file
Does anyone have an idea how to make it work?
Many thanks!
The code is as follows:
public string CreateEvent()
{
var fb = new FacebookWebClient();
Dictionary<string, object> createEventParameters = new Dictionary<string, object>();
createEventParameters.Add("name", "My birthday party )");
createEventParameters.Add("start_time", DateTime.Now.AddDays(2).ToUniversalTime().ToString(new CultureInfo("en-US")));
createEventParameters.Add("end_time", DateTime.Now.AddDays(2).AddHours(4).ToUniversalTime().ToString(new CultureInfo("en-US")));
createEventParameters.Add("owner", "Balaji Birajdar");
createEventParameters.Add("description", " ( a long description can be used here..)");
//Add the "venue" details for the event
JsonObject venueParameters = new JsonObject();
venueParameters.Add("street", "dggdfgg");
venueParameters.Add("city", "gdfgf");
venueParameters.Add("state", "gfgdfgfg");
venueParameters.Add("zip", "gfdgdfg");
venueParameters.Add("country", "gfdgfg");
venueParameters.Add("latitude", "100.0");
venueParameters.Add("longitude", "100.0");
createEventParameters.Add("venue", venueParameters);
createEventParameters.Add("privacy", "OPEN");
createEventParameters.Add("location", "fhdhdfghgh");
//Add the event logo image
//You can add the event logo too
FacebookMediaObject logo = new FacebookMediaObject()
{
ContentType = "image/jpeg",
FileName = #"C:\DevProjects\O2\o2PriorityFB\o2PriorityFB.Web\Images\logo.jpg"
};
logo.SetValue(System.IO.File.ReadAllBytes(logo.FileName));
createEventParameters["#file.jpg"] = logo;
JsonObject resul = fb.Post("/me/events", createEventParameters) as JsonObject;
return resul["id"].ToString();
}
You need to include a valid access token in the request. Also, I would recommend upgrading to V6 of the Facebook C# SDK. For the version you are using though you need to pass the access token into the constructor of FacebookWebClient as follows:
var fb = new FacebookWebClient("valid_facebook_access_token");
I am trying to upload photos form Windows phone (mango) to facebook with BackgroundTransferRequest object. This is because I want to upload photos even if the app is not running.
I am using facebook C# SDK to get access token etc and which works great but to upload using this, we need to keep the APP active.
I can use fiddler and create a Post request with access token and upload the image to facebook to a album.
https://graph.facebook.com/178040612256938/Photos/?access_token=AAACEdEose0cBAA4p0Ozqj0H39RP2tGyxdq0LAxoADyBZCPgjgrXMwU93VAOVkulemC3ZC5lVZCTiJ3rYeFXtM67tYNEZBvPQmasbT9AvX
Now, here is my code that I took off of sample here - http://msdn.microsoft.com/en-us/library/hh202959(v=vs.92).aspx
I am not sure how to add headers that fiddler adds OR how to use this for uploading photos to FB. getting 400 as response. Currently the following code can upload photo to my WCF service.
also looked at - BackgroundTransferRequest WP7
IsolatedStorageFileExtensions.SavePicture(Path.Combine(TransfersFiles, picture.FileName), picture.Data);
string fbURL = #"https://graph.facebook.com/106216062727932/Photos/?access_token=AAACEdEose0cBAA4p0Ozqj0H39RP2tGyxdq0LAxoADyBZCPgjgrXMwU93VAOVkulemC3ZC5lVZCTiJ3rYeFXtM67tYNEZBvPQmasbT9AvD";
var transferRequest = new BackgroundTransferRequest(new Uri(fbURL, UriKind.Absolute));
if (!_wifiOnly)
{
transferRequest.TransferPreferences = TransferPreferences.AllowCellular;
}
if (!_externalPowerOnly)
{
transferRequest.TransferPreferences = TransferPreferences.AllowBattery;
}
if (!_wifiOnly && !_externalPowerOnly)
{
transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
}
//this is the place to upload to Facebook
transferRequest.Method = "POST";
//_OLD transferRequest.UploadLocation = new Uri(TransfersFiles + #"\" + picture.FileName, UriKind.Relative);
transferRequest.UploadLocation = new Uri(TransfersFiles + #"\" + picture.FileName, UriKind.Relative);
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
//---
transferRequest.TransferStatusChanged += OnTransferStatusChanged;
transferRequest.TransferProgressChanged += OnTransferProgressChanged;
BackgroundTransferService.Add(transferRequest);