Change profile photo #103
Klaytonfratoni
started this conversation in
General
Replies: 2 comments 1 reply
-
Hi, i guess this can help in updating the business profile |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi, I have a code example in the project that does the resumable upload
that contains the h value
```C#
public async Task<IActionResult> UploadMedia(UploadMediaViewModel uploadMediaViewModel, IFormFile mediaFile)
{
try
{
var fileName = Path.GetFileName(mediaFile.FileName).Trim('"');
var rootPath = Path.Combine(_environment.WebRootPath, "Application_Files\\MediaUploads\\");
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
// Get the path of filename
var filePath = Path.Combine(_environment.WebRootPath, "Application_Files\\MediaUploads\\", fileName);
// Upload Csv file to the browser
using (var stream = new FileStream(filePath, FileMode.Create))
{
await mediaFile.CopyToAsync(stream);
}
if (uploadMediaViewModel.SelectedUploadType.Equals("Normal Upload", StringComparison.OrdinalIgnoreCase))
{
UploadMediaRequest uploadMediaRequest = new UploadMediaRequest();
uploadMediaRequest.File = filePath;
uploadMediaRequest.Type = mediaFile.ContentType;
var uploadMediaResult = await _whatsAppBusinessClient.UploadMediaAsync(uploadMediaRequest);
var mediaUrlResult = await _whatsAppBusinessClient.GetMediaUrlAsync(uploadMediaResult.MediaId);
var mediaBytes = await _whatsAppBusinessClient.DownloadMediaAsync(mediaUrlResult.Url);
ViewBag.MediaId = uploadMediaResult.MediaId;
}
else // Resumable upload generates header file response to be used for creating message templates
{
var resumableUploadMediaResult = await _whatsAppBusinessClient.CreateResumableUploadSessionAsync(mediaFile.Length, mediaFile.ContentType, mediaFile.FileName);
if (resumableUploadMediaResult is not null)
{
var uploadSessionId = resumableUploadMediaResult.Id;
var resumableUploadResponse = await _whatsAppBusinessClient.UploadFileDataAsync(uploadSessionId, filePath, mediaFile.ContentType);
var queryResumableUploadStatus = await _whatsAppBusinessClient.QueryFileUploadStatusAsync(uploadSessionId);
if (resumableUploadResponse is not null)
{
ViewBag.H = resumableUploadResponse.H;
}
if (queryResumableUploadStatus is not null)
{
ViewBag.StatusId = queryResumableUploadStatus.Id;
ViewBag.FileOffset = queryResumableUploadStatus.FileOffset;
}
}
}
return View(uploadMediaViewModel).WithSuccess("Success", "Successfully upload media.");
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
return RedirectToAction(nameof(UploadMedia)).WithDanger("Error", ex.Message);
}
}
```
…On Tue, Sep 10, 2024 at 6:30 PM Klaytonfratoni ***@***.***> wrote:
Hi @gabrieldwight <https://github.com/gabrieldwight>, thanks for
responding.
I've been trying to do this for days but I can't, could you help me?
I can't successfully send the image to Facebook, I wrote two functions
using the RestSharp library:
public async Task GetIdUploadFileAsync(string idapplication, string
fileName, string filelength, string fileType, string accessToken)
{
RestClient client = new RestClient();
var url = $"
https://graph.facebook.com/v20.0/{idapplication}/uploads?file_name={fileName}&file_length={filelength}&file_type={fileType}&access_token={accessToken}
";
var request = new RestRequest(url, Method.Post);
try
{
Debug.WriteLine("Sending request to Facebook API...");
var response = client.Execute(request);
Debug.WriteLine("Request completed.");
if (response.IsSuccessful)
{
var jsonResponse = JObject.Parse(response.Content);
return jsonResponse["id"]?.ToString();
}
else
{
throw new Exception($"Failed to upload file: {response.StatusDescription}");
}
}
catch (Exception ex)
{
Debug.WriteLineIf(true, ex.Message);
throw new Exception($"Failed to upload file");
}
}
This function successfully returns the id for the endpoint of the second
function:
public async Task<string> UploadFileAsync(string uploadSessionId, string userAccessToken, string fileName)
{
var url = $"https://graph.facebook.com/v20.0/{uploadSessionId}";
var client = new RestClient(url);
var request = new RestRequest();
request.AddHeader("Authorization", $"OAuth {userAccessToken}");
request.AddHeader("file_offset", "0");
byte[] fileBytes = File.ReadAllBytes(fileName);
request.AddParameter("application/octet-stream", fileBytes, ParameterType.RequestBody);
var response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine("File uploaded successfully.");
var jsonResponse = JObject.Parse(response.Content);
return jsonResponse["h"]?.ToString();
}
else
{
Console.WriteLine($"Failed to upload file: {response.StatusDescription}");
throw new Exception($"Failed to upload file");
}
}
This function returns another id, not the "h" that is expected as the
Facebook documentation says.
And finally the function that should publish the image on my number.
public async Task PostPhotoProfileAsync(string phoneNumberId, string
accessToken, string about, string address, string description, string
industry, string email, string[] websites, string profilePictureHandle)
{
var url = $"
https://graph.facebook.com/v20.0/{phoneNumberId}/whatsapp_business_profile
";
var client = new RestClient(url);
var request = new RestRequest();
request.AddHeader("Authorization", $"OAuth {accessToken}");
request.AddHeader("Content-Type", "application/json");
var body = new
{
messaging_product = "whatsapp",
//about,
//address,
//description,
//vertical = industry,
//email,
//websites,
profile_picture_handle = profilePictureHandle
};
request.AddJsonBody(body);
var response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine("Profile updated successfully.");
return response.Content;
}
else
{
Console.WriteLine($"Failed to update profile: {response.StatusDescription}");
throw new Exception($"Failed to update profile");
}
}
—
Reply to this email directly, view it on GitHub
<#103 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGWLN755DJNAREYCQSTGKVDZV4GCLAVCNFSM6AAAAABN5GB4CWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANRQGQ2DEMI>
.
You are receiving this because you were mentioned.Message ID:
<gabrieldwight/Whatsapp-Business-Cloud-Api-Net/repo-discussions/103/comments/10604421
@github.com>
--
*Thank You!*
*Gabriel Dwight Curtis Odero*
Email: ***@***.***
"Never Give Up"
"Believe yourself be the best"
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I couldn't figure out how to change my phone number photo via API. Can anyone help me? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions