Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class HomeController : Controller
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; //clientId
private static string appKey = ConfigurationManager.AppSettings["ida:AppKey"]; //clientSecret
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; //https://login.windows.net/{0}
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"]; //TenantName.onmicrosoft.com
private const string ResourceUri = "https://onenote.com";
public static readonly string Authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);//https://login.windows.net/TenantName.onmicrosoft.com
Uri returnUri = new Uri("https://localhost:44327/Home/ReturnFlow");
public ActionResult Index()
{
string authorizationUrl = string.Format(
aadInstance + "/oauth2/authorize?api-version=1.0&response_type=code&client_id={1}&resource={2}&redirect_uri={3}",
tenant,
clientId,
ResourceUri,
returnUri.AbsoluteUri
);
/*
https://login.windows.net/TenantName.onmicrosoft.com/oauth2/authorize?api-version=1.0&response_type=code
&client_id=9eaffd31-8592-4c96-892c-32adb9a432b8&resource=https://onenote.com&redirect_uri=https://localhost:44327/Home/ReturnFlow
*/
return new RedirectResult(authorizationUrl);
}
public async Task<ActionResult> ReturnFlow(string code)
{
AuthenticationContext AuthContext = new AuthenticationContext(Authority);
ClientCredential credential = new ClientCredential(clientId, appKey);
var result = await AuthContext.AcquireTokenByAuthorizationCodeAsync(code, returnUri, credential);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string apiRoute = "https://www.onenote.com/api/beta/me/notes/notebooks";
var getMessage = new HttpRequestMessage(HttpMethod.Get, apiRoute);
HttpResponseMessage response = await client.SendAsync(getMessage);
string data = await response.Content.ReadAsStringAsync();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.