How to create OAuth2 based connections via Peaka API?
Learn how to create OAuth2 based connections via Peaka API.
You can easily create connection via the Peaka API. For example, here’s how to create a connection to Stripe, a very popular payment platform:
Passing the Stripe token to the Peaka platform is sufficient.
Not all 3rd party platforms support a simple token (or user:password) based authentication. For example, HubSpot - a very popular CRM platform - or Google Sheets use OAuth2 based authentication (see: https://en.wikipedia.org/wiki/OAuth or https://auth0.com/intro-to-iam/what-is-oauth-2). The most common OAuth2 grant type is Authorization Code. Authorization Code flow requires some user interaction. After various steps, the platform you are using gives you a pair of token: an Access Token and a Refresh Token. Peaka should know these tokens in order to query data of the platform.
Peaka has already implemented various 3rd party platforms using OAuth2. If you are using Peaka
- directly (https://peaka.studio/),
- in an Embedded UI (https://docs.peaka.com/how-to-guides/how-to-embed-peaka-to-my-app),
you will get connected to those platforms.
For those who want to create connection based on OAuth2 via Peaka Rest Api, there are two options:
- Use your OAuth implementation, provide tokens to Peaka
- Use Peaka OAuth mechanism
1. Use Your OAuth Implementation
Partners may have an already implemented OAuth process and OAuth apps.
In order to use these apps, Peaka needs clientId, clientSecret, refreshToken and accessToken.
Since partner makes all OAuth flows, it is sufficient to pass these info to Peaka. This is the simplest integration way of OAuth.
Upon getting refresh token and access token from the OAuth Provider (e.g. Google), the Partner will make “create connection” request.
A typical request example will be
And sample response returning connection id will be:
Partners choose this way integration because they have already have Oauth processes or they want to do OAuth processes by themselves.
2. Use Peaka OAuth Mechanism
OAuth flows are a bit complex to implement for some partners. Defining and registering appropriate OAuth apps can also be complex and time consuming (e.g. Some platforms may have longer review process).
Peaka does the complex part and informs you when an OAuth flow has completed.
Since everything happens in the user browser, such integration is more complex than the previous.
Whether you will be using Peaka OAuth App or your own OAuth app, this mechanism can be implemented in two ways:
2.a. Use Peaka OAuth App
When you choose to use Peaka OAuth app for some Platform (e.g. Google Analytics), OAuth flow will end at Peaka site and Peaka will inform you after that.
Using Peaka OAuth app has some advantages and disadvantages. Also in some cases, you should use your own OAuth app.
Advantages:
- Some platforms enable public creation of an OAuth app, some requires developer accounts, and some requires a partnership program. Also they may have review process. Peaka has completed review process of the Platform you are using. So you don’t need to wait for your own.
Disadvantages:
- In OAuth flow, a consent screen will be shown to your user. In that screen, Peaka’s branding and logo will be shown. If you need to show your logo and branding, you should choose to create your own OAuth app and implement the next option.
- Platforms may have strict rate limits. To avoid rate limits, you may consider creating your own oauth app.
- Your user clicks a button in your application in order to create connection. e.g. Google Analytics connection. The response of this http request will be done in Step
-
- Your server should make a create connection request
Here partner_callback_url
is the full url of your server endpoint responsible to complete creation process. We will use partner_callback_url
at Step 10 and 11.
- As a response to the request in Step 2
Peaka will return the url of the consent screen of the Platform.
- Your server should return redirect:<platform_consent_url> to the browser as response to the request initiated at Step 1. Also it is a good practice to store
connection_id
and the location where the user has started the connection.
An example for Step 1-4 can be given in Node.js as follows:
(Note that it is not a complete working example)
-
Browser will automatically redirect to Platform Consent Screen.
-
Browser shows the Consent Screen.
-
User will approve the consents.
-
Platform will return the Peaka OAuth app’s redirect (oauth2 callback of Peaka) redirect:https://peaka.studio/oauth2/callback
-
Browser will be redirected to Peaka Callback.
-
Peaka will return a redirection to your
partner_callback_url
:<partnerCallback >?connectionId=<connectionId >?status=< success or fail >
-
The browser will be redirected to your
<partner_callback_url>
with query parametersconnectionId
andstatus
. Ifstatus
issuccess
, the connection has been created successfully. Otherwise, ifstatus
isfail
, it indicates a problem, such as a platform error or a timeout in the flow.
A typical implementation of Step 10 and Step 11 in Node.js:
(Note that it is not a complete working example)
2. b. Use Your Own OAuth App
You should create your own app,
- if you need to use your own branding and logo
- if you need better rate limits for the Platform API
- if Peaka supports connection for the Platform but does not have a global app.
- Here, global app means that the Platform allows Peaka use data of other users. Sometimes the platforms allow only non-global apps or there are long review processes for the global apps.
There are a couple things to consider while your are creating your OAuth app.
The OAuth app will ask for redirect url (or callback url). Redirect Url is the endpoint where the browser is redirected at the end of OAuth flow. According to the given url, the integration process will differentiate.
- You can use Peaka’s own callback url.
In that case, it is necessary and sufficient to implement steps described in 2.a. Use Peaka Oauth App with a minor diff. You should give the client id and client secret parameters in the request.
Step 2 will be:
- You can use your own redirect url and then redirect it to the Peaka’s callback.
If you do the appropriate redirection, the implementation steps will be same as the above case.
In that case, the user browser will momentarily redirected to Peaka’s callback and then it will be redirected to partner_callback_url
e.g. You give the OAuth app redirect_url
as https://yourdomain.com/oauth2/mycallback
Then you should redirect the request coming to your redirect_url
to https://peaka.studio/oauth2/callback
You can do this redirection in your reverse proxy server (Apache, Nginx, Traefik, etc.) or in your application server (Express in Node.js, Spring in Java, etc.).
Apache (HTTPD)
Nginx
Traefik
Node.js
- You can use your own callback url and the user’s browser will never redirected to Peaka’s domain.
This approach requires a different implementation compared to the previous cases when integrating your OAuth app.
- Your user clicks a button in your application in order to create connection. e.g. Google Analytics connection. The response of this http request will be done in Step 4.
- Your server should make a create connection request
Here redirect_url
under “credential” object should be the callback (or redirect) url you give to the Platform OAuth app.
- As a response to the request in (2)
Peaka will return the url of the consent screen of the Platform.
- Your server should return redirect:<platform_consent_url> to the browser as response to the request initiated at Step 1. Also it is a good practice to store
connection_id
and the location where the user has started the connection.
An example for Step 1-4 can be given in Node.js as follows: (Note that it is not a complete working example)
- Browser will automatically redirect to Platform Consent Screen
- Browser shows the Consent Screen.
- User will approve the consents.
- Platform will return the your OAuth app’s redirect url - you give in Step 2 - with some query parameters.
- Browser will be redirected to your callback with some query parameters.
- A typical
redirect_url
will be
You should pass all the query parameters to Peaka as follows:
- Response of Step 10 will be
A typical implementation of Step 10 and Step 11 in Node.js:
(Note that it is not a complete working example)