添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. Hi, I'm using this code below. Not getting ant try/catch errors but am getting this:
"Id = 9, Status = WaitingForActivation {1}, Method = "{null}", Result = "{Not yet computed}"
I've been stuck for many hours and can't seem to figure this out. I hope someone can help or provide a good example.
My task is to get MS oAuth to return a token so I can access an API.
Thank You
Code:
Public Class Tforce
    Inherits System.Web.UI.Page
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles form1.Load
        Dim Test = AsyncCall(Nothing)
    End Sub
    Protected Async Function AsyncCall(ByVal e As System.EventArgs) As Task
        Dim clientId As String = "4e502cbc-a55f-4341-a498-69cfbe19ee7b"
        Dim clientSecret As String = "My Secret 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_crdentials"},
                                            {"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
            responsemessage = Await client.SendAsync(requestMessage)
        Catch ex As Exception
            Dim showerror As String = ex.Message
        End Try
    End Function
End Class
You have a function that currently does not return anything. A function needs to return something..In your case, a Task(of something)...
So in this case, you can just return your response content...
Code:
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 Secret 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 responsemessage = Await client.SendAsync(requestMessage) ' Check if the response is successful If responsemessage.IsSuccessStatusCode Then Dim responseContent As String = Await responsemessage.Content.ReadAsStringAsync() ' Return the response content Return responseContent ' If response is not successful, return an error message or handle accordingly Return "Error: " & responsemessage.StatusCode.ToString() End If Catch ex As Exception ' If an exception occurs during the request, return the error message Return "Error: " & ex.Message End Try End Function had to add a page directive " async="true" to get the page to run.
Then I added a button to fire off the function. After about 2 min's I received the ex.message of "Error: A task was canceled."
This is weird. I pasted your code in and added a button to call the function
Protected Async Sub BtnGo_Click(sender As Object, e As EventArgs) Handles BtnGo.Click
Dim Test = Await AsyncCall(Nothing)
End Sub
Aghhh
any ideas? Well, at this point you need to debug your code and figure out what is happening. Set some breakpoints inside your try..catch and then step through after the breakpoint is hit.
If I had to guess, I would say you are running into a timeout on your httpclient. Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.