APIs & Integrations

jivago
Member

Get Access Token in a Console application (No user interface)

I need to figure out a way to get the access token without prompting authorization to the user, we have another application that needs to post events to our hubspot portal without prompting to select a hubsportal, etc. It is a console application.

Is there a way to do it?

0 Upvotes
12 Replies 12
Not applicable

Get Access Token in a Console application (No user interface)

Here in the below code i am getting access token using client id and client secret of an azure app and i am building context. using context i am unable to get data from share point site.
class Program
{
static void Main(string[] args)
{
test().Wait();
}
private static async Task test()
{
string authorityUri = "https://login.windows.net/common/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authorityUri);
ClientCredential clientCredentials = new ClientCredential(clientID,clientSecret);
AuthenticationResult token = await authContext.AcquireTokenAsync("App ID URI", clientCredentials);
Console.WriteLine(token);
using (var context = TokenHelper.GetClientContextWithAccessToken("https://pranthi-admin.sharepoint.com", token.AccessToken))
{
Web rootweb = context.Web;
var test = rootweb.AllProperties;
context.Load(test);
context.ExecuteQuery();
Console.WriteLine(test);
}
return token;
}
}

At "context.ExecuteQuery();" i am getting exception like "{"The remote server returned an error: (401) Unauthorized."}".
How to resolve this.......

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Get Access Token in a Console application (No user interface)

Hi @kamisetty_tejyaprant,

That code doesn't appear to be initiating an OAuth connection with HubSpot. This forum is intended to provide community support for the HubSpot APIs, so I'm not really equipped to assist with troubleshooting authentication issues with any external services. If anyone here in the community has experience with Azure/Sharepoint, I look forward to seeing the conversation continue!

0 Upvotes
jonbarthol
Member

Get Access Token in a Console application (No user interface)

Thanks again @dadams. I was actually able to get the form submission to work. The sample C# code was extremely helpful!

0 Upvotes
jonbarthol
Member

Get Access Token in a Console application (No user interface)

Thank you @dadams. This is helpful. If I instead use an API key, do I even need a custom integration? Wouldn’t my external web form just make a call to https://api.hubapi.com/contacts/v1/contact?hapikey=xxxxxxxx-xxx-xxx-xxx-xxxxxxxxx? There’s no client id or client secret involved in this case. Thanks again.

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Get Access Token in a Console application (No user interface)

@jonbarthol correct, you do not need to create an app in HubSpot or go through the authorization process if you’re using a hapikey.

0 Upvotes
jonbarthol
Member

Get Access Token in a Console application (No user interface)

@dadams I’m hoping you can point me in the right direction. We have an e-commerce site that any guest user may use to sign up for a trial subscription. The guest user will not be a HubSpot user, but will provide basic info like name and email. We want to use HubSpot to store the guest as a contact. When the guest submits the form for a trial subscription, we want our e-commerce site to make an API call to https://api.hubapi.com/contacts/v1/contact to add a new contact. From what you posted to @jivago it sounds like we first need to get a refresh token by signing into HubSpot using our admin account. After getting the refresh token, we can use that going forward, so that guests aren’t asked to sign into HubSpot. We would need to store the refresh token somewhere for future use. Does this sound right? Thanks so much.

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Get Access Token in a Console application (No user interface)

@jonbarthol this is basically correct. If you’re using OAuth to authenticate with the Contacts API, you’d need to log into HubSpot and authorize your account to get the initial access token and refresh token. Once you have the refresh token, you can use that to generate new access tokens when the access tokens expire.

If you’re working on a custom integration that’s only intended for your use, you could instead use an API key for your portal:

How do I get my HubSpot API key?

Marketing Basic, Professional, and Enterprise customers and portals with a CRM can access the HubSpot API. Learn how to access your HubSpot API key here.

Also, if your visitors are filling out a form, you could also use the Forms API to send the relative marketing info to HubSpot.

Submit data to a form | HubSpot Forms API

POST https://forms.hubspot.com/uploads/form/v2/:portal_id/:form_guid - Send form submission data to HubSpot. Form submissions from external sources can be made to any registered HubSpot form. You can see a list of forms on your portal by going to...

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Get Access Token in a Console application (No user interface)

Hi @jivago

Authenticating an app using OAuth currently requires a HubSpot user to log into HubSpot and click the Grant access button. There isn’t a way to programmatically grant access, so you’d need to have a user go through that process to at least get the code, Once you have that code, you can get the access_token and refresh_token, and you’d be able to use the refresh token to generate new access tokens going forward.

0 Upvotes
jivago
Member

Get Access Token in a Console application (No user interface)

Thank you @dadams!

That helps a lot! Do you know if the refresh token expires? Do I need to update it every time i request a new access_token?

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Get Access Token in a Console application (No user interface)

Refresh tokens do not expire, so you’d continue to use the same refresh token each time the access_token expires.

0 Upvotes
jivago
Member

Get Access Token in a Console application (No user interface)

Awesome! You saved me! Thank you so much!

0 Upvotes
jonbarthol
Member

Get Access Token in a Console application (No user interface)

@jivago were you able to get your console application to work? I have a similar issue where I have a third-party web application in which I need to programmatically add contacts in my HubSpot portal. Guest users of my third-party web application will not be HubSpot users. Any help would be appreciated. Thanks!

0 Upvotes