Created
October 30, 2019 12:41
-
-
Save vman/03c66d8958a38d23c5f3bcec1244811c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Graph; | |
using Microsoft.Identity.Client; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
namespace Graph.SPO.Thumbnails | |
{ | |
class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
GraphServiceClient graphClient = await GetGraphClient(); | |
//Get the site object. You can use other ways to get the site object as well: | |
//https://docs.microsoft.com/en-us/graph/api/site-get?view=graph-rest-1.0&tabs=http | |
Site site = await graphClient.Groups["<your-office365-group-id>"].Sites["root"].Request().GetAsync(); | |
//Get the modern page thumbnails | |
var queryOptions = new List<QueryOption> | |
{ | |
new QueryOption("expand", "thumbnails"), | |
new QueryOption("select", "thumbnails/large") //large, medium, small | |
}; | |
var driveItem = await graphClient.Sites[site.Id].Lists["Site Pages"].Drive.Root.ItemWithPath($"/MySPOModernPage.aspx").Request(queryOptions).GetAsync(); | |
var thumbnail = driveItem.Thumbnails.First(); | |
Console.WriteLine(thumbnail?.Large?.Url); | |
} | |
private static async Task<GraphServiceClient> GetGraphClient() | |
{ | |
var scopes = new string[] { "https://graph.microsoft.com/.default" }; | |
var clientID = "<client-id>"; | |
var clientSecret = "<client-secret>"; | |
var tenantID = "<tenant-id>"; | |
IConfidentialClientApplication clientApp = ConfidentialClientApplicationBuilder | |
.Create(clientID) | |
.WithClientSecret(clientSecret) | |
.WithTenantId(tenantID) | |
.Build(); | |
AuthenticationResult authResult = await clientApp.AcquireTokenForClient(scopes).ExecuteAsync(); | |
string accessToken = authResult.AccessToken; | |
var graphClient = new GraphServiceClient( | |
new DelegateAuthenticationProvider( | |
requestMessage => | |
{ | |
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken); | |
return Task.CompletedTask; | |
})); | |
return graphClient; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment