Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using this code (below), Trying to obtain an access key via oAuth2 .
Request runs fir about 2 mins then throws and error "request was cancelled".
This is driving me nuts. Could use a second set of eyes and help.
-Thanks


ASP.NET
<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


What I have tried:

added several try catch blocks. No luck.
Posted
Comments
Graeme_Grant 20-Apr-24 18:45pm    
There is more to that error message than what is indicated. Look at the raw response. Also, you need to check how you have configured your account on Microsoft's side as it will prevent you from authenticating if not configured correctly.
danash 22-Apr-24 15:04pm    
I'm not sure how to view the raw respose. I've exposed the error in my immediate window in my dev environment but it's not saying much. I'd appreciate any help.I'm stuck and dead in the water.
Graeme_Grant 24-Apr-24 4:51am    
You have caught the error. Set a breakpoint where you return the error and inspect the rest of the error, not just the message.
danash 24-Apr-24 11:44am    
Thanks You. I did that earlier, guess I should have posted it. All I'm seeing is below and isn't telling me much as far as I know.

?ex
{"A task was canceled."}
CancellationToken: IsCancellationRequested = True
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233029
HelpLink: Nothing
InnerException: Nothing
Message: "A task was canceled."
Source: "mscorlib"
StackTrace: " at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)" & vbCrLf & " at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)" & vbCrLf & " at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()" & vbCrLf & " at FMS_FR2022.Tforce.VB$StateMachine_12_AsyncCall.MoveNext() in C:\Users\dan\source\repos\FMS-FR2022\Tforce.aspx.vb:line 124"
TargetSite: {Void ThrowForNonSuccess(System.Threading.Tasks.Task)}
Task: Id = 52, Status = Canceled {6}, Method = "{null}", Result = "{Not yet computed}"
Graeme_Grant 24-Apr-24 16:24pm    
Are you sure that is the correct exception message? That would indicate that the request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

1 solution

The problem was my Dev environment was hooked to Starlink satillite Internet which uses a NAT (network address translation) to household consolidate my IP address into one outbound IP. I pulled the ethernet cable from my desktop and used my Iphone hotspot and everything worked fine. It may be possible to submit a redirect uri in the post request and get the response back that way. We shall see. At least I'm hgetting a response now over the hot spot
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900