Hi,
Authentication code:
public static GraphServiceClient GetAuthenticatedClient()
{
if (graphClient == null)
{
// Create Microsoft Graph client.
try
{
graphClient = new GraphServiceClient(
$"https://graph.microsoft.com/v1.0/",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
var token = await GetTokenForUserAsync();
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
}));
return graphClient;
}
catch (Exception ex)
{
Debug.WriteLine("Could not create a graph client: " + ex.Message);
}
}
return graphClient;
}
public static async Task<string> GetTokenForUserAsync()
{
AuthenticationResult authResult;
var accounts = await IdentityClientApp.GetAccountsAsync();
try
{
authResult = await IdentityClientApp.AcquireTokenSilent(Scopes, accounts.FirstOrDefault()).ExecuteAsync();
TokenForUser = authResult.AccessToken;
}
catch (Exception)
{
if (TokenForUser == null || Expiration <= DateTimeOffset.UtcNow.AddMinutes(5))
{
authResult = await IdentityClientApp.AcquireTokenInteractive(Scopes).ExecuteAsync();
TokenForUser = authResult.AccessToken;
Expiration = authResult.ExpiresOn;
}
}
return TokenForUser;
}
And this is the code which tries to get site icon:
string url = $"https://{tenantname}.sharepoint.com/sites/{sitename}/_api/siteiconmanager/getsitelogo?type=1";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36";
request.Headers.Add( "Authorization", new AuthenticationHeaderValue("bearer", AuthenticationHelper.TokenForUser).ToString());
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
System.Drawing.Image image = Bitmap.FromStream(stream);
}
}