APIs & Integrations

lester
Member

How to get OAuth token for non-Web app

I’m trying to create a Console App (using C#), but I’m receiving the following error message:

{“status”:“BAD_GRANT_TYPE”,“message”:“missing or unknown grant type”,“correlationId”:“d68b4a8d-e8a3-47b6-a526-61f685532668”,“requestId”:“2d3ffd8ee0502ec9ff6442b8f2b61b1f”}

The C# code I’m using to get the access token is as follows:

var client = new RestClient(“https://api.hubapi.com/oauth/v1/token”);
var request = new RestRequest(Method.POST);
request.AddHeader(“cache-control”, “no-cache”);
request.AddHeader(“content-type”, “application/x-www-form-urlencoded”);
request.AddParameter(“application/x-www-form-urlencoded”, “grant_type=client_credentials&client_id=” + clientId + “&client_secret=” + clientSecret + “&scope=contacts%20forms”, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Note that I cannot specify a redirect_uri to receive the auth_code, as I’m trying to use the API from a Console App.

0 Upvotes
7 Replies 7
davidhch
Member

How to get OAuth token for non-Web app

Hi Lester

I think there is no way round your problem, Hubspot seem to support response_type=code only. I can see what you want to achieve.

Could you create a mini project thats a UI sitting on front of the console app, supplying the console app with an access token, storing the token in a shared location?

David

0 Upvotes
lester
Member

How to get OAuth token for non-Web app

Hi David,

Yes, It seems this is the only option available.
I just wonder in what context it could be used the app client secret?

Thanks,
Lester

0 Upvotes
davidhch
Member

How to get OAuth token for non-Web app

Hi Lester

I was thinking you can utilise the flexible nature of Hubspot’s OAuth implementation. Hubspot will allow you to use https://localhost:xxyyzz/ (xxyyzz is the app’s port) as the redirect_uri. So if you were able to instantiate a cut down browser or http parser within your console app, it could obtain the redirect.

Make sense?

David

0 Upvotes
lester
Member

How to get OAuth token for non-Web app

Hi David,

If I understood well, your suggestion is to create a self-hosted web app within the console app and listen for the POST from HubSpot. I’m not sure this is the workflow I need.

I’m trying to create a library (DLL) to encapsulate the HubSpot API as well as the OAuth workflow to obtain and refresh the token. The client apps that use this library will only need to call a method such as CreateContact without worry about security, which will be handled internally from the DLL.

In this scenario, app-app, there is no user intervention. Therefore, the granting access through a dialog is not an option. This is why I’m trying to use the client secret to authenticate/authorise the app. As far as I understand, the code I’m using should work, but it seems that HubSpot doesn’t support the grant_type client_credentials, to fulfil the authorisation needs in the scenario described.

I’m unsure about how to proceed, to obtain the oauth token without the user intervention, from a non-web app.

Lester

0 Upvotes
kencox
Member

How to get OAuth token for non-Web app

Did you ever get a resolution to this app-to-app Oauth issue?

I’m trying to figure out how a 100% server-side process (MVC) can use Oauth in HubSpot. Starting to look like an impossibility. If the tokens expire, there’s nobody to go in and re-authorize.

0 Upvotes
davidhch
Member

How to get OAuth token for non-Web app

Hi lester

There are OAuth flows that dont use a redirect_uri, but I dont think Hubspot offer any other flow.

Could you instantiate a browser object to accept a redirect_uri - i think this could be the answer for you - Hubspot’s implementation of grant type OAuth is quite flexible - you could specify it to be your console app’s address. Hubspot’s requirement is that this redirect_urii has to be across ssl.

Hope this helps

David

0 Upvotes
lester
Member

How to get OAuth token for non-Web app

Hi David,

Thanks for your reply, but I’m not sure how to implement your suggestion. As I’m trying to get the token from a Console App, I don’t see how I can pass a redirect URL to receive the code, to then get the token. Could you please clarify the workflow you suggest?

Thanks,
Lester

0 Upvotes