kroo / reflex-clerk

A reflex custom component library for clerk
20 stars 10 forks source link

Allow Notion SSO and requesting OAuth Access Tokens #14

Open dentro-innovation opened 3 months ago

dentro-innovation commented 3 months ago

Today I tried setting up Notion SSO using reflex-clerk.

As it seems, the only thing that was needed for that is in Commit 1.

Commit 2 is meant for requesting OAuth access tokens.

I need this to get access to the users notion and e.g. create new pages.

The method get_user_oauth_access_token isn't clean at all, and it just happens to work with Notion on my machine:

I got it to work on my own machine with the following functions using the pypi package notion_client:

from notion_client import Client

async def fetch_notion_data(self):
        try:
            token = await self.get_oauth_access_token("oauth_notion")

            # Use the token to fetch data from Notion API
            # This is a placeholder and should be replaced with actual Notion API call
            notion_response = requests.get(
                "https://api.notion.com/v1/users",
                headers={
                    "Authorization": f"Bearer {token}",
                    "Notion-Version": "2022-06-28",
                }
            )
            notion_data = notion_response.json()
            print(notion_data)
        except Exception as e:
            print(e)

    async def create_notion_page(self):
        try:
            token = await self.get_oauth_access_token("oauth_notion")
            notion = Client(auth=token)

            parent_page = notion.search(query="OMG Is it the Notion API?!")
            # pprint(parent_page)
            parent_page_id = parent_page["results"][0]["id"]
            print(parent_page_id)

            new_page = notion.pages.create(
                parent={"page_id": parent_page_id},
                properties={
                    "title": [{"type": "text", "text": {"content": "New page created via API"}}]
                },
                children=[
                    {
                        "object": "block",
                        "type": "paragraph",
                        "paragraph": {
                            "rich_text": [{"type": "text", "text": {"content": "This is a paragraph created using the Notion API."}}]
                        }
                    },
                    {
                        "object": "block",
                        "type": "bulleted_list_item",
                        "bulleted_list_item": {
                            "rich_text": [{"type": "text", "text": {"content": "This is a bullet point."}}]
                        }
                    },
                    {
                        "object": "block",
                        "type": "to_do",
                        "to_do": {
                            "rich_text": [{"type": "text", "text": {"content": "This is a to-do item."}}],
                            "checked": False
                        }
                    }
                ]
            )

            print(new_page)
        except Exception as e:
            print(e)

To be able to actually write into the users Notion I couldn't use Clerks Dev Notion Integration but needed to create a public integration myself with write permissions: image

Then I filled in the Client ID and Secret in Clerk, and set the scopes to read, insert and update, and copied the Authorized redirect URI back into the Notion Integration: image

With this whole setup I was able to create a new page under my parent page as shown above in the python code.

I'd be very happy if you could try it out yourself, improve the code and then release a new version 🙏 Thanks for maintaining this project so far @kroo !

dentro-innovation commented 3 months ago

After a talk with the guys from clerk on their discord, it seems like how I set up the oauth retrieval isn't quite the best practice because after signing up it doesn't redirect me back to my app: https://discord.com/channels/856971667393609759/1275507212059541574/1275908575675551764