I cannot get an access token using the HubSpot api. I can get the initial HubSpot consent pages to load, I can grant consent from the popups, and I get the return code, but when taking the next step and passing along that code with my client id, client secret, etc. to exchange for a token, I get "BAD REQUEST" no matter what I try. I am following the quickstart guide, but need to use C# (4.8 framework) without any external NuGet packages. Oauth is an open standard and I have an identical working solution calling and getting a token from AdobeStock but the HubSpot api is not accepting the same parameters. I've tried calling in a few different ways, here is the latest (non-working attempt). Is there a sure-fire working C# example posted anywhere that does NOT use RestSharp or other Nuget packages?
var encodedData = new FormUrlEncodedContent(data);
FormUrlEncodedContent is part of System.Net.Http and works only with .NET Core or newer versions of .NET Framework when referencing the correct NuGet packages. However, it is known to sometimes serialize the body with a charset in the Content-Type header (e.g., application/x-www-form-urlencoded; charset=utf-8)—and HubSpot's OAuth token endpoint does not accept that
var encodedData = new FormUrlEncodedContent(data);
with this will help
var encodedDataString = string.Join("&", data.Select(kvp =>
$"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"));
var encodedData = new StringContent(encodedDataString, Encoding.UTF8, "application/x-www-form-urlencoded");
The Content-Type is exactly what HubSpot expects: application/x-www-form-urlencoded (no charset). if you're still getting a Bad Request even after fixing the encoding, their token endpoint always includes a JSON error message that tells you what went wrong
The method that you are using is for C#, but HubSpot is a JavaScript-based environment, so you have to change the encoded data.
var encodedData = new FormUrlEncodedContent(data); so this should be like
const encodedData = new URLSearchParams(data).toString();
You can then use it in a fetch() request:
fetch('https://example.com/api', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: encodedData
});
I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member. Thanks!
var encodedData = new FormUrlEncodedContent(data);
FormUrlEncodedContent is part of System.Net.Http and works only with .NET Core or newer versions of .NET Framework when referencing the correct NuGet packages. However, it is known to sometimes serialize the body with a charset in the Content-Type header (e.g., application/x-www-form-urlencoded; charset=utf-8)—and HubSpot's OAuth token endpoint does not accept that
Is there some other code sample using C# that works that you can provide? I've tried using StringContent instead of FormUrlEncodedContent, but got the same BAD REQUEST.
var encodedData = new FormUrlEncodedContent(data);
with this will help
var encodedDataString = string.Join("&", data.Select(kvp =>
$"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"));
var encodedData = new StringContent(encodedDataString, Encoding.UTF8, "application/x-www-form-urlencoded");
The Content-Type is exactly what HubSpot expects: application/x-www-form-urlencoded (no charset). if you're still getting a Bad Request even after fixing the encoding, their token endpoint always includes a JSON error message that tells you what went wrong