<pre> Protected Async Sub BtnGo_Click(sender As Object, e As EventArgs) Handles BtnGo.Click Dim Test As String = Await AsyncCall(Nothing) End Sub Protected Async Function AsyncCall(ByVal e As System.EventArgs) As Task(Of String) Dim clientId As String = "4e502cbc-a55f-4341-a498-69cfbe19ee7b" Dim clientSecret As String = "my sercret goes here" Dim credentials = String.Format("{0}:{1}", clientId, clientSecret) Dim headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)) Dim content = New FormUrlEncodedContent(New Dictionary(Of String, String) From { {"client_id", clientId}, {"client_secret", clientSecret}, {"grant_type", "client_credentials"}, {"scope", "https://tffproduction.onmicrosoft.com/04cc9749-dbe5-4914-b262-d866b907756b/.default"} }) Dim requestMessage = New HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/ca4f5969-c10f-40d4-8127-e74b691f95de/oauth2/v2.0/token") requestMessage.Headers.Authorization = New AuthenticationHeaderValue("Basic", headerValue) requestMessage.Content = content Dim responsemessage As HttpResponseMessage Dim client As New HttpClient Try responsemessage = Await client.SendAsync(requestMessage) If responsemessage.IsSuccessStatusCode Then Dim responseContent As String = Await responsemessage.Content.ReadAsStringAsync() Return responseContent Else Return "Error: " & responsemessage.StatusCode.ToString() End If Catch ex As Exception Return "Error: " & ex.Message End Try End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)