Thursday, April 25, 2024
HomeProgrammingEasy methods to Create a Public Notion Integration Utilizing Python | by...

Easy methods to Create a Public Notion Integration Utilizing Python | by Norah Klintberg Sakal | Oct, 2022


Combating creating public Notion integrations and gaining access to different customers’ Notion databases? Right here’s a Python implementation

With this walk-through, you’ll be capable to authenticate customers, get their entry token, and add information to the database they’ve shared together with your public Notion integration.

Right here’s what we’ll be utilizing:

  1. Python
  2. Notion API
  1. Create public Notion integration
  2. Create a desk to share
  3. Create Notion Auth 2.0 authorization circulate
  4. Add information to a licensed desk

Let’s get going.

Let’s begin with making a public Notion integration.

Begin creating an integration by logging into your Notion account, going to https://www.notion.so/my-integrations, and clicking on the + New integration button.

Give your integration a reputation and choose the workspace the place you need to set up this integration. We’ll improve the combination to make use of OAuth later.

Then select the capabilities that the combination may have. Lastly, click on Submit to create the combination:

After getting saved the combination, scroll right down to Integration kind and click on on Public integration:

It will add a brand new part, OAuth Area & URIs, which requires you to supply extra information about your public integration.

Let’s go forward and add all of the required information, beginning with Redirect URIs. Because it says in Notion:

Within the Notion OAuth circulate, customers shall be redirected to this path after they’ve authenticated with Notion. The trail shall be appended with the authorization code for entry and will need to have a protocol. It could actually’t comprise URL fragments, relative paths or wildcards, and may’t be a public IP tackle. It should even be included within the token request.

For studying, this tutorial will copy the authorization code proper from the browser URL. However in your manufacturing app, you’ll want to supply a redirect URL the place you possibly can extract the authorization code out of your customers.

Proceed with including your Firm identify and your Web site or homepage — this web site/homepage shall be utilized in your integration web page as said by Notion:

Used to hyperlink to your integration’s web site or homepage in your integration web page and authentication screens.

Additional, fill out a tagline and go forward and add the hyperlinks to a privateness coverage, phrases of use, and eventually, a help electronic mail. All these fields are required since you’re about to create a public integration obtainable to all Notion customers.

While you’ve crammed out all of the fields, click on on Submit:

Make certain to save lots of the OAuth consumer ID, OAuth consumer secret, and Authorization URL for the following steps. Keep in mind that you’ll solely be capable to reveal the OAuth consumer secret as soon as, so make certain to put it aside someplace for the following step.

That’s step one. Let’s transfer on to create a brand new database to share with our integration afterward.

This step is to create a brand new database that we’ll authorize our public integration to entry.

Let’s create a desk with 2 columns, deal with and tweet:

Select textual content as the sort for the column tweet. As you may guess — it is a desk we’ll populate with tweets later as a use case instance.

That’s all we’d like for this database. Let’s head to the following part, the place we’ll create the Notion 2.0 authorization circulate for customers.

On this part, we’ll write to code wanted to generate

  1. a URL that grants your public integration entry to chosen databases/pages and
  2. an entry token from the code we’ll obtain within the authorization redirect

Begin by importing all of the libraries and creating variables in your API keys that we obtained in Notion in step 1:

import base64
import json
import os
import requests
import urllib.parse
oauth_client_id = "YOUR_OAUTH_CLIENT_ID"
oauth_client_secret = "YOUR_OAUTH_CLIENT_SECRET"
redirect_uri = "YOUR_REDIRECT_URL"

Parse your redirect URL:

parsed_redirect_uri = urllib.parse.quote_plus(redirect_uri)

The subsequent step is to create the authorization URL that authorizes your public integration entry to customers’ pages/databases:

auth_url = f"https://api.notion.com/v1/oauth/authorize?proprietor=consumer&client_id={oauth_client_id}&redirect_uri={parsed_redirect_uri}&response_type=code"print(auth_url)

This hyperlink will immediate customers to authorize your public integration and provide you with entry to their chosen Notion pages and databases.

Click on on the hyperlink after which Choose pages. Right here’s what you and the consumer sees when visiting your URL:

If the consumer grants entry, they are going to be proven a web page the place they will search and choose which pages and databases they need to share together with your integration.

Now choose the Notion database that we created earlier, it ought to be known as Twitter, that is what you ought to be seeing:

Click on on Enable entry and you ought to be redirected to your redirect_uri.

If customers enable entry, they’ll even be redirected to your redirect_uri with a short lived authorization code. It’ll look one thing like this:

https://YOUR_REDIRECT_URL/?code=9ac813er-34ad-4ba9-a5a0-52u6&state= 
|--- Your redirect URL --| |--- temp authorization code ---|

Save the entire URL that you just simply acquired redirected to after permitting authorization:

redirect_url_response = "THE_URL_YOU_GOT_REDIRECTED_TO_AFTER_AUTHORIZATION"

Then extract and save the code you obtain within the redirect URL:

auth_code = redirect_url_response.cut up('code=')[-1].cut up('&state=')[0]auth_code

After the consumer grants entry, we have to trade the authorization code for an entry token by sending an HTTP POST request to Notion’s token URL https://api.notion.com/v1/oauth/token.

The request is permitted with HTTP Fundamental Authentication, so that you’ll have to base64 encode your integration’s credentials first:

key_secret = '{}:{}'.format(oauth_client_id, oauth_client_secret).encode('ascii')
b64_encoded_key = base64.b64encode(key_secret)
b64_encoded_key = b64_encoded_key.decode('ascii')

The final step on this authorization circulate is to trade the momentary code for an entry token:

Notion will reply to the request with an entry token and a few extra info.

Right here’s what the response seems to be like:

Retailer all the info your integration receives with the entry token. Now you can use this entry token to succeed in all of the pages and databases the consumer granted entry to.

Begin by trying to find which databases the consumer granted entry to:

url = "https://api.notion.com/v1/search"

payload = {"page_size": 100}
headers = {
"settle for": "utility/json",
"Notion-Model": "2022-06-28",
"content-type": "utility/json",
"authorization": f"Bearer {access_token}"
}

response = requests.publish(url, json=payload, headers=headers)

Right here’s what the response seems to be like:

Save the database id for whenever you need to add information within the subsequent step:

database_data = response.json()['results']
notion_database_id = database_data[0]['id']

Nice, now now we have all of the logic to ask customers to grant entry to their pages and databases in Notion. The final step is including information to the desk the consumer gave you entry to.

Begin by creating some instance/dummy information:

example_data = {
"deal with": "@SomeHandle",
"tweet": "Here's a tweet"
}

Then add the info to the approved Notion database:

If we return to the Notion database we created in step 2, you’ll see the enter we simply made added to the database:

  • Create public Notion integration — We began by making a public Notion integration and saved all of the OAuth credentials
  • Create a desk to share — Continued with making a desk to which we might grant entry.
  • Create Notion Auth 2.0 authorization circulate — Lastly, we created the OAuth circulate for customers granting entry to their pages and databases.
  • Add information to a licensed desk — Lastly, we added information utilizing the entry token we obtained when a consumer granted our integration entry to their pages and databases.

Right here is the repo with a Jupyter pocket book with all of the supply code when you’d wish to implement this by yourself.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments