Share via

Not able to REST call to https://graph.microsoft.com/beta/appCatalogs/teamsApps

Anonymous
2024-05-06T09:03:33.39+00:00

Hi Team,

I need sample code in C# or documentation how can I generate token in C# code and by using that token how can I hit the URL: and make the request to that API

https://graph.microsoft.com/beta/appCatalogs/teamsApps

Microsoft Security | Microsoft Graph
0 comments No comments

2 answers

Sort by: Most helpful
  1. Wilson Ricardo Olarte Martínez 21 Reputation points
    2024-05-06T13:21:57.0633333+00:00

    HI @Anonymous

    i can share my code for generate token with c#

    private const string clientID = "xxxxxxxxxxxxxxxxxxx";
    private const string aadInstance = "https://login.microsoftonline.com/{0}";                
    private const string tenant = "contosoexampledomainclient.onmicrosoft.com";                
    private const string resource = "https://graph.windows.net/";                
    private const string appKey = "xxxxxxxxxxxxxxxxxxx";
    static string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
    private static HttpClient httpClient = new HttpClient();
    var app = ConfidentialClientApplicationBuilder.Create(clientID)
        .WithLegacyCacheCompatibility(false)
        .WithAuthority(authority)
        .WithClientSecret(appKey)
        .Build();
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
    var url = "";
    string teamsApps = null;
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    url = ("https://graph.microsoft.com/beta/appCatalogs/teamsApps");                
    var getResult = await httpClient.GetAsync(url);
    teamsApps = await getResult.Content.ReadAsStringAsync();
    

    In getResult you found the result of get API Microsoft.

    With httpClient you found the token of microsoft.

    Note important: appKey = AppSecret in azure.

    If the reply is helpful, please click Accept Answer and kindly upvote it.

    Was this answer helpful?

    0 comments No comments

  2. CarlZhao-MSFT 46,456 Reputation points
    2024-05-06T10:03:51.9066667+00:00

    Hi @Hamendra Kumar (HCL Technologies Ltd)

    Refer to the following code snippet:

    using Microsoft.Graph;
    using Azure.Identity;
    
    
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    
    var tenantId = "xxxxxxxxxxxxxxxxxx";
    
    var clientId = "xxxxxxxxxxxxxxxxx";
    
    var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx";
    
    // using Azure.Identity; 
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    
    /*var token = await clientSecretCredential.GetTokenAsync(new TokenRequestContext(scopes));
    Console.WriteLine($"Access token: {token.Token}");*/
    
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
    var result = await graphClient.AppCatalogs.TeamsApps.GetAsync();
    
    Console.WriteLine(result);
    

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.