{"id":28394438,"url":"https://github.com/authlib/aiohttp-oauth-client","last_synced_at":"2025-06-27T00:31:38.330Z","repository":{"id":57408925,"uuid":"207975038","full_name":"authlib/aiohttp-oauth-client","owner":"authlib","description":"Use HTTPX instead: https://docs.authlib.org/en/latest/client/httpx.html","archived":false,"fork":false,"pushed_at":"2024-06-10T16:01:29.000Z","size":16,"stargazers_count":19,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-01T05:23:46.457Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/authlib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-12T05:54:37.000Z","updated_at":"2024-11-06T16:50:42.000Z","dependencies_parsed_at":"2022-09-26T17:11:06.814Z","dependency_job_id":null,"html_url":"https://github.com/authlib/aiohttp-oauth-client","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/authlib/aiohttp-oauth-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authlib%2Faiohttp-oauth-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authlib%2Faiohttp-oauth-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authlib%2Faiohttp-oauth-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authlib%2Faiohttp-oauth-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/authlib","download_url":"https://codeload.github.com/authlib/aiohttp-oauth-client/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/authlib%2Faiohttp-oauth-client/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262166146,"owners_count":23268991,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-31T18:11:37.959Z","updated_at":"2025-06-27T00:31:38.299Z","avatar_url":"https://github.com/authlib.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aiohttp OAuth Clients\n\n\n## OAuth 1.0 Client\n\nTaking Twitter as an example on how to use `OAuth1Client`.\n\n```python\nfrom aiohttp_oauth_client import OAuth1Client\n```\n\n### 1. Fetch Temporary Credential\n\nFor OAuth 1.0, the first step is requesting a temporary credential (aka\nrequest token).\n\n\n```python\nclient_id = '__YOUR_TWITTER_CLIENT_ID__'\nclient_secret = '__YOUR_TWITTER_CLIENT_SECRET__'\n\nasync def fetch_request_token():\n    url = 'https://api.twitter.com/oauth/request_token'\n\n    async with OAuth1Client(client_id, client_secret) as session:\n        request_token = await session.fetch_request_token(url)\n        return request_token\n```\n\nThis `fetch_request_token` method will return the temporary credential like:\n\n```\n{'oauth_token': 'Ih....Jw', 'oauth_token_secret': 'gr...GE', 'oauth_callback_confirmed': 'true'}\n```\n\nWe can test this function with:\n\n```py\n# twitter.py\n\nimport asyncio\nfrom aiohttp_oauth_client import OAuth1Client\n\nclient_id = '__YOUR_TWITTER_CLIENT_ID__'\nclient_secret = '__YOUR_TWITTER_CLIENT_SECRET__'\n\nasync def fetch_request_token():\n    url = 'https://api.twitter.com/oauth/request_token'\n\n    async with OAuth1Client(client_id, client_secret) as session:\n        request_token = await session.fetch_request_token(url)\n        return request_token\n\n\nasync def main():\n    request_token = await fetch_request_token()\n    print(request_token)\n\nif __name__ == '__main__':\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(main())\n```\n\n\n### 2. Redirect to Authorization Endpoint\n\nThe second step is to generate the authorization URL:\n\n```py\ndef create_authorization_url(request_token):\n    authenticate_url = 'https://api.twitter.com/oauth/authenticate'\n    client = OAuth1Client(client_id, client_secret)\n    url = client.create_authorization_url(authenticate_url, request_token['oauth_token'])\n    return url\n```\n\nThis step is synchronized, no need for `async` and `await`. Passing the\n`request_token` we got from the first step, we would get a url like:\n\n```\nhttps://api.twitter.com/oauth/authenticate?oauth_token=Ih....Jw\n```\n\nThen visit this URL with your browser, and approve the request. Twitter\nwill redirect back to your default `redirect_uri` you registered in Twitter.\n\nIf you want to redirect to your specified URL, rewrite the code like:\n\n```py\ndef create_authorization_url(request_token):\n    authenticate_url = 'https://api.twitter.com/oauth/authenticate'\n    client = OAuth1Client(client_id, client_secret, redirect_uri='https://your-defined-url')\n    url = client.create_authorization_url(authenticate_url, request_token['oauth_token'])\n    return url\n```\n\n### 3. Fetch Access Token\n\nThe last step is to fetch the access token. In previous step, twitter\nwill redirect back to a URL, for instance, something like:\n\n```\nhttps://your-domain.org/auth?oauth_token=Ih...Jw\u0026oauth_verifier=fcg..1Dq\n```\n\nWe will use the `oauth_verifier` in this URL to fetch access token. Here\nis our code:\n\n\n```py\nasync def fetch_access_token(request_token, oauth_verifier):\n    url = 'https://api.twitter.com/oauth/access_token'\n    async with OAuth1Client(client_id, client_secret) as session:\n        session.token = request_token\n        token = await session.fetch_access_token(url, oauth_verifier)\n        return token\n\n\n# if you specified redirect_uri in previous step, create the session with\n# OAuth1Client(client_id, client_secret, redirect_uri='....')\n```\n\nWe can test this function with:\n\n```py\n# twitter.py\n\nimport asyncio\nfrom aiohttp_oauth_client import OAuth1Client\n\nclient_id = '__YOUR_TWITTER_CLIENT_ID__'\nclient_secret = '__YOUR_TWITTER_CLIENT_SECRET__'\n\nasync def fetch_access_token(request_token, oauth_verifier):\n    url = 'https://api.twitter.com/oauth/access_token'\n    async with OAuth1Client(client_id, client_secret) as session:\n        session.token = request_token\n        token = await session.fetch_access_token(url, oauth_verifier)\n        return token\n\nasync def main():\n    # from step 1\n    request_token = {\n        'oauth_token': 'Ih....Jw',\n        'oauth_token_secret': 'gr...GE',\n        'oauth_callback_confirmed': 'true'\n    }\n    # from step 2\n    verifier = 'fcg..1Dq'\n    token = await fetch_access_token(request_token, verifier)\n    print(request_token)\n\nif __name__ == '__main__':\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(main())\n```\n\n\n## OAuth 2.0 Client\n\n```python\nfrom aiohttp_oauth_client import OAuth2Client\n\n```\n\n\n## OAuth 2.0 Assertion\n\n```python\nfrom aiohttp_oauth_client import AssertionClient\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fauthlib%2Faiohttp-oauth-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fauthlib%2Faiohttp-oauth-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fauthlib%2Faiohttp-oauth-client/lists"}