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.
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.......
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!
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.
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:
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.
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...
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.
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!