Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,069 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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}");
}