APIs & Integrations

MSpeers
Participant

OAuth Not Working C#

SOLVE

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? OAuthHubSpot.png

2 Accepted solutions
FrancoQ
Solution
Contributor

OAuth Not Working C#

SOLVE

Hey, the issue is likely with this line:

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

View solution in original post

FrancoQ
Solution
Contributor

OAuth Not Working C#

SOLVE

Hi! yes! @MSpeers

Replacing this  

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

View solution in original post

5 Replies 5
GRajput
Recognized Expert | Gold Partner
Recognized Expert | Gold Partner

OAuth Not Working C#

SOLVE

Hi @MSpeers 

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!




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting


0 Upvotes
FrancoQ
Solution
Contributor

OAuth Not Working C#

SOLVE

Hey, the issue is likely with this line:

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

MSpeers
Participant

OAuth Not Working C#

SOLVE

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.

0 Upvotes
FrancoQ
Solution
Contributor

OAuth Not Working C#

SOLVE

Hi! yes! @MSpeers

Replacing this  

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

MSpeers
Participant

OAuth Not Working C#

SOLVE

Thank you @FrancoQ . This got it working.