8000 feat: Docs update and examples for organization api uptake and public… · CoolNightTimeCoder/twilio-python@0b8aab4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b8aab4

Browse files
authored
feat: Docs update and examples for organization api uptake and public oauth (twilio#825)
* Docs update and examples
1 parent 6e78c78 commit 0b8aab4

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ After a brief delay, you will receive the text message on your phone.
8686
> **Warning**
8787
> It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out [How to Set Environment Variables](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) for more information.
8888
89+
## OAuth Feature for Twilio APIs
90+
We are introducing Client Credentials Flow-based OAuth 2.0 authentication. This feature is currently in beta and its implementation is 8000 subject to change.
91+
92+
API examples [here](https://github.com/twilio/twilio-python/blob/main/examples/public_oauth.py)
93+
94+
Organisation API examples [here](https://github.com/twilio/twilio-python/blob/main/examples/organization_api.py)
95+
8996
## Use the helper library
9097

9198
### API Credentials

examples/organization_api.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
3+
from twilio.rest import Client
4+
from twilio.credential.orgs_credential_provider import OrgsCredentialProvider
5+
6+
ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID")
7+
API_KEY = os.environ.get("TWILIO_API_KEY")
8+
API_SECRET = os.environ.get("TWILIO_API_SECRET")
9+
10+
CLIENT_ID = os.environ.get("TWILIO_CLIENT_ID")
11+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
12+
ORGS_SID = os.environ.get("ORGS_SID")
13+
14+
15+
def example():
16+
"""
17+
Some example usage of using organization resources
18+
"""
19+
self.client = Client(
20+
account_sid=ACCOUNT_SID,
21+
credential_provider= OrgsCredentialProvider(CLIENT_ID,CLIENT_SECRET)
22+
)
23+
24+
accounts = self.client.preview_iam.organization(organization_sid=ORGS_SID).accounts.stream()
25+
for record in accounts:
26+
print(record)
27+
28+
29+
if __name__ == "__main__":
30+
example()

examples/public_oauth.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
3+
from twilio.rest import Client
4+
from twilio.credential.client_credential_provider import ClientCredentialProvider
5+
6+
ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID")
7+
API_KEY = os.environ.get("TWILIO_API_KEY")
8+
API_SECRET = os.environ.get("TWILIO_API_SECRET")
9+
FROM_NUMBER = os.environ.get("TWILIO_FROM_NUMBER")
10+
TO_NUMBER = os.environ.get("TWILIO_TO_NUMBER")
11+
12+
CLIENT_ID = os.environ.get("TWILIO_CLIENT_ID")
13+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
14+
15+
16+
def example():
17+
"""
18+
Some example usage of message resources.
19+
"""
20+
self.client = Client(
21+
account_sid=ACCOUNT_SID,
22+
credential_provider= ClientCredentialProvider(CLIENT_ID,CLIENT_SECRET)
23+
)
24+
25+
msg = self.client.messages.create(
26+
to=self.to_number, from_=self.from_number, body="hello world"
27+
)
28+
29+
30+
if __name__ == "__main__":
31+
example()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from twilio.http.client_token_manager import ClientTokenManager
2+
from twilio.base.exceptions import TwilioException
3+
from twilio.credential.credential_provider import CredentialProvider
4+
from twilio.auth_strategy.auth_type import AuthType
5+
from twilio.auth_strategy.token_auth_strategy import TokenAuthStrategy
6+
7+
8+
class ClientCredentialProvider(CredentialProvider):
9+
def __init__(self, client_id: str, client_secret: str, token_manager=None):
10+
super().__init__(AuthType.CLIENT_CREDENTIALS)
11+
12+
if client_id is None or client_secret is None:
13+
raise TwilioException("Client id and Client secret are mandatory")
14+
15+
self.grant_type = "client_credentials"
16+
self.client_id = client_id
17+
self.client_secret = client_secret
18+
self.token_manager = token_manager
19+
self.auth_strategy = None
20+
21+
def to_auth_strategy(self):
22+
if self.token_manager is None:
23+
self.token_manager = ClientTokenManager(
24+
self.grant_type, self.client_id, self.client_secret
25+
)
26+
if self.auth_strategy is None:
27+
self.auth_strategy = TokenAuthStrategy(self.token_manager)
28+
return self.auth_strategy

twilio/http/client_token_manager.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from twilio.http.token_manager import TokenManager
2+
from twilio.rest import Client
3+
4+
5+
class ClientTokenManager(TokenManager):
6+
"""
7+
Client Token Manager
8+
"""
9+
10+
def __init__(
11+
self,
12+
grant_type: str,
13+
client_id: str,
14+
client_secret: str,
15+
code: str = None,
16+
redirect_uri: str = None,
17+
audience: str = None,
18+
refreshToken: str = None,
19+
scope: str = None,
20+
):
21+
self.grant_type = grant_type
22+
self.client_id = client_id
23+
self.client_secret = client_secret
24+
self.code = code
25+
self.redirect_uri = redirect_uri
26+
self.audience = audience
27+
self.refreshToken = refreshToken
28+
self.scope = scope
29+
self.client = Client()
30+
31+
def fetch_access_token(self):
32+
token_instance = self.client.preview_iam.v1.token.create(
33+
grant_type=self.grant_type,
34+
client_id=self.client_id,
35+
client_secret=self.client_secret,
36+
code=self.code,
37+
redirect_uri=self.redirect_uri,
38+
audience=self.audience,
39+
scope=self.scope,
40+
)
41+
return token_instance.access_token

0 commit comments

Comments
 (0)
0