(Apologies if this is in the wrong category.)
I am trying to get the Access Token and Refresh Token for the first time using the method found here in Visual Basic:
Private Shared Function GetOAuthAccessAndRefreshTokens() As String Dim strResp As String = "" Try Dim grantType As String = "authorization_code" Dim redirectURI As String = "https://www.hubspot.com/" Dim postData As String = "grant_type=authorization_code&client_id=" & HubClientID & "&client_secret=" & ClientSecret & "&redirect_uri=" & redirectURI & "&code=" & authorizationCode Dim byteReq() As Byte = Encoding.UTF8.GetBytes(postData) Dim objReq As WebRequest = WebRequest.Create("https://api.hubapi.com/oauth/v1/token") objReq.Method = "POST" objReq.ContentLength = byteReq.Length objReq.ContentType = "application/x-www-form-urlencoded" Dim OutStream As IO.Stream = objReq.GetRequestStream() OutStream.Write(byteReq, 0, byteReq.Length) OutStream.Close() Dim objResp As WebResponse = objReq.GetResponse() 'This is where it keeps erroring out with a 400 response. Dim sr As New IO.StreamReader(objResp.GetResponseStream(), Encoding.UTF8, True) strResp = sr.ReadToEnd() sr.Close() objResp.GetResponseStream().Close() objResp.Close() Catch ex As System.Net.WebException Throw New ArgumentException("Error SendRequest: " + ex.Message + " " + ex.Source) End Try Return strResp End Function
I have the client ID, client secret, and authorization code saved in private shared global variables, and the redirect URI here is the same one I used when I first authorized my app. I have checked and am confident that these values are inserted into the strings correctly.
Whenever I run this, I get an error thrown at the line denoted with the comment; everything before this line runs correctly. I have tried countless variations with both versions of postData but nothing is working. I feel like there is something simple that i am missing but i have no idea what.
EDIT: Figured I should mention some of the variations i’ve tried:
- Including/excluding ;charset=“utf-8” from the end of the ContentType string
- Including/excluding the closing forward-slash on both the post URL and the redirectURI
- formatting postData as a JSON object and using ContentType of “application/json”