Skip to content

Commit

Permalink
adding example for custom oauth2 storage to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jpetrucciani committed Dec 7, 2019
1 parent 0d50970 commit 1c022ab
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,41 @@ if __name__ == "__main__":
print(json.dumps(a.get_pipelines()))
```

# Advanced oauth2 token storage - thanks [@sangaline](https://github.com/sangaline)\!

This is an example of how you can use the `oauth2_token_getter` and `oauth2_token_setter` kwargs on the client to use custom storage (in this case redis) so that multiple clients can share the same access/refresh tokens generated by the oauth2 requests.

```python
import aioredis
from hubspot3 import Hubspot3


redis_client = await aioredis.create_redis_pool(url, db=db, encoding='utf-8', timeout=10)

def oauth2_token_getter(token_type: str, client_id: str) -> str:
loop = asyncio.get_event_loop()
key = f'hubspot-oauth2-tokens:{token_type}:{client_id}'
return loop.run_until_complete(redis_client.get(key))

def oauth2_token_setter(token_type: str, client_id: str, token: str) -> None:
loop = asyncio.get_event_loop()
key = f'hubspot-oauth2-tokens:{token_type}:{client_id}'
# Token expiration is six hours, so match that when we store the tokens.
# See: https://developers.hubspot.com/docs/methods/oauth2/refresh-access-token
expire_in_seconds = 6 * 60 * 60
loop.run_until_complete(redis_client.set(key, token, expire=expire_in_seconds))

# This client will share oauth2 credentials with other clients configured in the same way.
hubspot3_client = Hubspot3(
access_token=access_token,
client_id=client_id,
client_secret=client_secret,
refresh_token=refresh_token,
oauth2_token_getter=oauth2_token_getter,
oauth2_token_setter=oauth2_token_setter,
)
```

# Testing

I'm currently working on rewriting many of the tests with
Expand Down

0 comments on commit 1c022ab

Please sign in to comment.