Im trying to get Users Email with Graph AD in xamarin forms, so after i login using Azure b2c and get the token i make the http request with get method using this https://graph.windows.net/me?api-version=1.6 but i didnt get the email i trying to get, here is my full code for that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Newtonsoft.Json.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace DesignKGVC
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GetUsersPage : ContentPage
{
public GetUsersPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
// let's see if we have a user in our belly already
try
{
var result = await App.AuthenticationClient.AcquireTokenSilentAsync(Constants.Scopes);
GetUsersInfo(result.Token);
await DisplayAlert("OK", "OK", "OK");
}
catch
{
// doesn't matter, we go in interactive more
}
}
private async void btnlogi(object sender, EventArgs e)
{
try
{
var result = await App.AuthenticationClient.AcquireTokenAsync(
Constants.Scopes,
string.Empty,
UiOptions.SelectAccount,
string.Empty,
null,
Constants.Authority,
Constants.SignUpSignInPolicy);
GetUsersInfo(result.Token);
// await DisplayAlert("OK", "OK", "OK");
}
catch (Exception)
{
//
}
}
public async void GetUsersInfo(string token)
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get,
"https://graph.windows.net/me?api-version=1.6");
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);
//new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.Token);
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
JObject user = JObject.Parse(content);
lbldisplayname.Text = user["displayName"].ToString();
lblmail.Text = user["otherMails"].ToString();
// just in case
// btnSignInSignOut.Text = "Sign out";
}
else
{
lbldisplayname.Text = "Api Call Dont Work";
//DisplayAlert("Something went wrong with the API call", responseString, "Dismiss");
}
}
}
}
and instead of geting display name and email, im geting "Api Call Dont Work" that i wrote in else condition so that i assume my Http Request is not succed, so what make that happen ? I tough there is no thing that i miss and i'm already get token that i send as parameter , or maybe b2c dont support Graph Api ?
EDIT
this is what i get when im using App Registration in Active Directory