Hi
I have a webpage protected by Microsoft Identity that I want to convert to PDF using C# and OAUTH for Authorization. Is this supported? Please provide OAUTH2 Authorization Code Example in C#.
I have tried to set up all the app registrations and scopes and everything.
I have modified the sample code example to this:
// C# Code
if (!string.IsNullOrEmpty(accessToken))
{
HtmlLoadOptions options = new HtmlLoadOptions(webApiUrl)
{
PageInfo = { Width = 842, Height = 1191, IsLandscape = true }
};
var defaultRequestHeaders = HttpClient.DefaultRequestHeaders;
defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await HttpClient.GetAsync(webApiUrl);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStreamAsync();
Document pdfDocument = new Document(content, options);
pdfDocument.Save($"test.PDF");
StreamReader reader = new StreamReader(content);
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Failed to call the web API: {response.StatusCode}");
string content = await response.Content.ReadAsStringAsync();
// Note that if you got reponse.Code == 403 and reponse.content.code == "Authorization_RequestDenied"
// this is because the tenant admin as not granted consent for the application to call the Web API
Console.WriteLine($"Content: {content}");
}
}
But the PDF document remains empty. Tried to print out the HTML String returned by the httpClient and it is the right content. If OAuth is supported - could you please show an example of how to do it?