Forum Discussion
KJS
Jun 30, 2017Brass Contributor
User Information or send mail with Graph API httpclient.SendAsync error
I am trying to build my first Graph API app. I have a console app and have registered the app in Azure AD.
Steps i followed till now
In Azure
1) After registering the app i can get the Application ID ( client ID ) and added a key so key value as ( client secret).
2) Added a test user with global admin role to AD
3) Added the same user to Owners
4) under Required Permission added graph api with Read /write access to user and mails ( Application and delegate permissions )
Console App
1) Called a get access token function
2) Then used the token with http client to make a request to graph url
public static string GetAccessToken() { // Step: Get the access token // Create the authentication context (ADAL) var authenticationContext = new AuthenticationContext(Authority); // Get the access token //KJ //var authenticationResult = authenticationContext.AcquireToken(GraphResource, // ClientId, RedirectUri, PromptBehavior.RefreshSession); ClientCredential clientCred = new ClientCredential(AppModeConstants.ClientId, AppModeConstants.ClientSecret); //var authenticationResult = authenticationContext.AcquireTokenAsync(GraphResource, ClientId, RedirectUri, new PlatformParameters(0)).Result.AccessToken; var authenticationResult = authenticationContext.AcquireTokenAsync(GraphResource, clientCred).Result.AccessToken; var accessToken = authenticationResult; return accessToken; } public static async Task<string> GetMyEmailAddress() { string accessToken = Model.MailClient.GetAccessToken(); // Get the current user. // The app only needs the user's email address, so select the mail and userPrincipalName properties. // If the mail property isn't defined, userPrincipalName should map to the email for all account types. string endpoint = "https://graph.microsoft.com/v1.0/me"; string queryParameter = "?$select=mail,userPrincipalName"; UserInfo me = new UserInfo(); using (var client = new HttpClient()) { using (var request = new HttpRequestMessage(HttpMethod.Get, endpoint + queryParameter)) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); // This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove. //request.Headers.Add("SampleID", "aspnet-connect-rest-sample"); using (var response = await client.SendAsync(request)) { if (response.IsSuccessStatusCode) { var json = JObject.Parse(await response.Content.ReadAsStringAsync()); //me.Address = !string.IsNullOrEmpty(json.GetValue("mail").ToString()) ? json.GetValue("mail").ToString() : json.GetValue("userPrincipalName").ToString(); } //return me.Address?.Trim(); return "temp"; } } } }
But i get no response back as i execute
client.SendAsync(request)
I also tried with other url but to no use, Can someone help with this.
Thanks,
KJ
- Amo ZhangCopper Contributor
Suggested to download the sample code from github and have a look. Or watch a startup in youtube. It won't take more than your troubleshooting.