Need to get access token using azure function app to run the SharePoint Rest API

Sivakoteswara Rao Sadhu 6 Reputation points
2024-04-17T14:28:01.2166667+00:00

Hi,

I have created Azure app and provided all the required application permissions to the app Eg: Sites.ReadWrite.All.

below is the code where i am using the in the function app

I need to get the Obtained_code from the authoraizationURL. and it should be done vai code, But i cant get it. Can you give me how to get that by code? I have followed this link. https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http

#r "Newtonsoft.Json"

using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string tenantId = "73cb413d-1111-1111-1111-1115ae862e59";

    string clientId = "38bee7b2-1111-1111-1111-111e29cb9882";
    string clientSecret = "rN08Q~111111111cKdve4fmcfy";
    string obtained_code ="0.ASsAPUHLc2E8F026BXvVroYuWbLnvjiSLlNHm900DinLmILCAL8.AgABBAIAAADnfolhJpSnRYB1SVj";
    
    string authorizationUrl = $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/authorize?" +
                               $"client_id={clientId}" +
                               "&response_type=code" +
                               "&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F" +
                               "&response_mode=query" +
                               "&scope=offline_access%20user.read%20Sites.Read.All" +
                               "&state=12345";

    
    HttpClient httpClient = new HttpClient();
    HttpResponseMessage authorizationResponse = await httpClient.GetAsync(authorizationUrl);
    string authorizationResponseContent = await authorizationResponse.Content.ReadAsStringAsync();


    string tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token";
    var tokenRequestBody = new Dictionary<string, string>
    {
        {"client_id", clientId},
        {"client_secret", clientSecret},
        {"code", obtained_code}, 
        {"scope", "https://mylocal.sharepoint.com/.default"},
        {"redirect_uri", "http://localhost/myapp/"},
        {"grant_type", "authorization_code"}
    };
    var tokenContent = new FormUrlEncodedContent(tokenRequestBody);

    
    HttpResponseMessage tokenResponse = await httpClient.PostAsync(tokenEndpoint, tokenContent);
    string tokenResponseContent = await tokenResponse.Content.ReadAsStringAsync();

    
    dynamic tokenData = JsonConvert.DeserializeObject(tokenResponseContent);
    string accessToken = tokenData.access_token;

    return new OkObjectResult($"{accessToken}");
}

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,069 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,618 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,025 questions
{count} votes

Your answer

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