Oauth Authorization - request returns 'the underlying connection was closed: an unexpected error occurred on a send'


#1

I’m trying to get a token from Asana through the Oauth authorization process but the request.GetResponse call returns ‘the underlying connection was closed: an unexpected error occurred on a send’. I’ve tried using the same uri that I’ve contructed in code directly in a browser and it works! This is in a ASP.NET project. Any help would be greatly appreciated.

    Dim uri As String = String.Format("https://app.asana.com/-/oauth_authorize")

    Dim parameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
    parameters.Add("client_id", thisClientID)
    parameters.Add("redirect_uri", thisAppRedirectURL)
    parameters.Add("response_type", "token")
    parameters.Add("state", "auth")

    Dim bodyParameters As List(Of String) = New List(Of String)
    For Each kv As KeyValuePair(Of String, Object) In parameters
        bodyParameters.Add(String.Format("{0}={1}", HttpUtility.UrlEncode(kv.Key), HttpUtility.UrlEncode(kv.Value.ToString())))
    Next

    Dim requestBody As String = String.Join("&", bodyParameters.ToArray())

    uri = String.Format("{0}?{1}", uri, requestBody)

    Dim request As HttpWebRequest = WebRequest.Create(uri)
    request.Method = "GET"
    request.ContentType = "application/x-www-form-urlencoded"
   
    Dim response As HttpWebResponse = request.GetResponse()

#2

FWIW if you want an alternative to rolling your own, I’m using this library to do my Asana OAuth:

That’s for ASP.NET Core, but I’m pretty sure there’s an equivalent library available for regular ASP.NET as well if you need that.