Skip to content

Commit 320613a

Browse files
authored
✨ Feature: support oauth token auth (#115)
1 parent 8f41ba1 commit 320613a

File tree

7 files changed

+1127
-604
lines changed

7 files changed

+1127
-604
lines changed

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ from githubkit import GitHub, OAuthAppAuthStrategy
135135
github = GitHub(OAuthAppAuthStrategy("<client_id_here>", "<client_secret_here>"))
136136
```
137137

138+
or using GitHub APP / OAuth APP token authentication (This is usefull when you stored the user token in a database):
139+
140+
```python
141+
from githubkit import GitHub, OAuthTokenAuthStrategy
142+
143+
github = GitHub(
144+
OAuthTokenAuthStrategy(
145+
"<client_id_here>",
146+
"<client_secret_here>",
147+
"<access_token_here>",
148+
"<access_token_expire_time_here>",
149+
"<refresh_token_here>",
150+
"<refresh_token_expire_time_here>",
151+
)
152+
)
153+
```
154+
138155
or using GitHub APP / OAuth APP web flow authentication:
139156

140157
```python
@@ -147,6 +164,22 @@ github = GitHub(
147164
)
148165
```
149166

167+
or using GitHub APP / OAuth APP device flow authentication:
168+
169+
```python
170+
from githubkit import GitHub, OAuthDeviceAuthStrategy
171+
172+
# sync/async func for displaying user code to user
173+
def callback(data: dict):
174+
print(data["user_code"])
175+
176+
github = GitHub(
177+
OAuthDeviceAuthStrategy(
178+
"<client_id_here>", callback
179+
)
180+
)
181+
```
182+
150183
or using GitHub Action authentication:
151184

152185
```python
@@ -503,7 +536,7 @@ from githubkit import GitHub
503536
event = GitHub.webhooks("2022-11-28").parse(request.headers["X-GitHub-Event"], request.body)
504537
```
505538

506-
### Switch between AuthStrategy
539+
### Switch between AuthStrategy (Installation, OAuth Web/Device Flow)
507540

508541
You can change the auth strategy and get a new client simplely using `with_auth`.
509542

@@ -518,13 +551,69 @@ installation_github = github.with_auth(
518551
)
519552
```
520553

521-
Change from `OAuthAppAuthStrategy` to `OAuthWebAuthStrategy`:
554+
Change from `OAuthAppAuthStrategy` to `OAuthWebAuthStrategy` (OAuth Web Flow):
522555

523556
```python
524557
from githubkit import GitHub, OAuthAppAuthStrategy
525558
526559
github = GitHub(OAuthAppAuthStrategy("<client_id>", "<client_secret>"))
527560
user_github = github.with_auth(github.auth.as_web_user("<code>"))
561+
562+
# now you can act as the user
563+
resp = user_github.rest.users.get_authenticated()
564+
user = resp.parsed_data
565+
566+
# you can get the user token after you maked a request as user
567+
user_token = user_github.auth.token
568+
user_token_expire_time = user_github.auth.expire_time
569+
refresh_token = user_github.auth.refresh_token
570+
refresh_token_expire_time = user_github.auth.refresh_token_expire_time
571+
```
572+
573+
you can also get the user token directly without making a request (Change from `OAuthWebAuthStrategy` to `OAuthTokenAuthStrategy`):
574+
575+
```python
576+
auth: OAuthTokenAuthStrategy = github.auth.as_web_user("<code>").exchange_token(github)
577+
# or asynchronously
578+
auth: OAuthTokenAuthStrategy = await github.auth.as_web_user("<code>").async_exchange_token(github)
579+
user_token = auth.token
580+
user_token_expire_time = auth.expire_time
581+
refresh_token = auth.refresh_token
582+
refresh_token_expire_time = auth.refresh_token_expire_time
583+
584+
user_github = github.with_auth(auth)
585+
```
586+
587+
Change from `OAuthDeviceAuthStrategy` to `OAuthTokenAuthStrategy`:
588+
589+
```python
590+
from githubkit import GitHub, OAuthDeviceAuthStrategy
591+
592+
def callback(data: dict):
593+
print(data["user_code"])
594+
595+
user_github = GitHub(OAuthDeviceAuthStrategy("<client_id>", callback))
596+
597+
# now you can act as the user
598+
resp = user_github.rest.users.get_authenticated()
599+
user = resp.parsed_data
600+
601+
# you can get the user token after you maked a request as user
602+
user_token = user_github.auth.token
603+
user_token_expire_time = user_github.auth.expire_time
604+
refresh_token = user_github.auth.refresh_token
605+
refresh_token_expire_time = user_github.auth.refresh_token_expire_time
606+
607+
# you can also exchange the token directly without making a request
608+
auth: OAuthTokenAuthStrategy = github.auth.exchange_token(github)
609+
# or asynchronously
610+
auth: OAuthTokenAuthStrategy = await github.auth.async_exchange_token(github)
611+
user_token = auth.token
612+
user_token_expire_time = auth.expire_time
613+
refresh_token = auth.refresh_token
614+
refresh_token_expire_time = auth.refresh_token_expire_time
615+
616+
user_github = github.with_auth(auth)
528617
```
529618

530619
## Development

0 commit comments

Comments
 (0)