1

I'm trying get thumbnail for pictures stored in document library in SharePoint online. But even simple call to site hangs.

I've the app registered with Microsoft to use application authentication. It has site.Read.All access

I'm using nugget packages Microsoft.Graph(v1.16) and Microsoft.Graph.Auth (v1.0.0-preview.0)

This is my first try to use Graph APIs. Any help is appreciated

I tried this using the Microsoft Graph explorer and the calls works fine.

https://graph.microsoft.com/v1.0/sites/{tanant}.sharepoint.com:/sites/{sitename}:/lists/33cda4ab-3019-4b95-b44f-4a3d845373f6/items/5/driveItem/thumbnails

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using System.Web.Http;
using Serilog;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System.Net.Http.Headers;

namespace MarketingPortfolioAPI.Helpers
{
internal static class MicrosofGraphAPIHelper
{
 private static GraphServiceClient _authClient = null;

private static readonly string clientId = ConfigurationManager.AppSettings["Graph.ClientId"];
private static readonly string tenantId = ConfigurationManager.AppSettings["Graph.TenantId"];
private static readonly string redirectUri = ConfigurationManager.AppSettings["Graph.RedirectUri"];
private static readonly string clientSecret = ConfigurationManager.AppSettings["Graph.ClientSecret"];
private static readonly string adminScopes = ConfigurationManager.AppSettings["Graph.AdminScopes"];

public static ListItem GetListItemAsync()
{

  //var queryOptions = new List<QueryOption>()
  //      {
  //        new QueryOption("expand", "fields")
  //      };

  var site = GraphClient.Sites["root"]
    .Request()
    .GetAsync().Result;

  ListItem listItem = null;
  //ListItem listItem = graphClient.Sites["MyTenant.sharepoint.com"].SiteWithPath("sites/Marketing").Lists["33cda4ab-3019-4b95-b44f-4a3d845373f6"].Items["5"]
  //  .Request()
  //  .GetAsync().Result;

  return listItem;
}

private static GraphServiceClient GraphClient
{
  get
  {
    if (_authClient == null)
    {
      _authClient = GetAuthenticatedGraphClient();
    }
    return _authClient;
  }
}

/// <summary>
/// Get authenticated graph client object
/// </summary>
/// <returns></returns>
internal static GraphServiceClient GetAuthenticatedGraphClient()
{

  string[] scopes = adminScopes.Split(new char[] { ' ' });
  string authority = string.Format("https://login.microsoftonline.com/{0}/v2.0", tenantId);

  GraphServiceClient graphClient = null;

  //IPublicClientApplication clientApp;
  //clientApp = PublicClientApplicationBuilder.Create(clientId)
  //              .WithAuthority(AzureCloudInstance.AzurePublic, tenantId)
  //              .Build();

  try
  {

    IConfidentialClientApplication cca;
    cca = ConfidentialClientApplicationBuilder.Create(clientId)
            .WithAuthority(authority)
            .WithRedirectUri(redirectUri)
            .WithClientSecret(clientSecret)
            .Build();

    // Build the Microsoft Graph client. As the authentication provider, set an async lambda
    // which uses the MSAL client to obtain an app-only access token to Microsoft Graph,
    // and inserts this access token in the Authorization header of each API request. 
    graphClient =
        new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
        {

          // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
          var authResult = await cca
              .AcquireTokenForClient(scopes)
              .ExecuteAsync();

          // Add the access token in the Authorization header of the API request.
          requestMessage.Headers.Authorization =
              new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
        })
        );


  }
  catch (Exception e)
  {
    Log.Error("MicrosofGraphAPIHelper.GetAuthenticatedGraphClient", e);
  }

  return graphClient;
}
}
}

My call get hangs on thenter code heree code below. I check through fiddler. There is authentication call which get the accessToken but there nothing after that

 var site = GraphClient.Sites["root"]
    .Request()
    .GetAsync().Result;
Guri
  • 11
  • 4
  • 1
    What kind of app is this? It's possible your call to `Result` is triggering a deadlock - have a read over the following question and see if it's relevant: https://stackoverflow.com/questions/17248680/await-works-but-calling-task-result-hangs-deadlocks – Brad Jul 26 '19 at 17:26
  • @Brad Thanks for replying. This is standard web api v2 application. I'll looking into this – Guri Jul 28 '19 at 15:30
  • 1
    @Brad Thanks I got it working. I had to async to all the methods from bottom to top. I was following Github example (https://github.com/microsoftgraph/dotnetcore-console-sample/tree/master/base-console-app) earlier – Guri Jul 30 '19 at 19:32

0 Answers0