8000 Adding examples and pushing client token manager and cred provider · twilio/twilio-python@eafd8dd · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eafd8dd

Browse files
committed
Adding examples and pushing client token manager and cred provider
1 parent 3670f03 commit eafd8dd

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-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 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_calls.py)
95+
8996
## Use the helper library
9097

9198
### API Credentials

examples/organization_api_calls.py

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

examples/public_oauth.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
3+
from twilio.rest import Client
4+
from twilio.credential.client_credential_provider import ClientCredentialProvider
5+
6+
API_KEY = os.environ.get("TWILIO_API_KEY")
7+
API_SECRET = os.environ.get("TWILIO_API_SECRET")
8+
ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID")
9+
CLIENT_ID = os.environ.get("TWILIO_CLIENT_ID")
10+
CLIENT_SECRET = os.environ.get("TWILIO_CLIENT_SECRET")
11+
ORGS_SID = os.environ.get("TWILIO_ORGS_SID")
12+
FROM_NUMBER = os.environ.get("TWILIO_FROM_NUMBER")
13+
TO_NUMBER = os.environ.get("TWILIO_TO_NUMBER")
14+
15+
16+
def example(self=None):
17+
"""
18+
Some example usage of fetching accounts within an organization
19+
"""
20+
self.client = Client(
21+
username=API_KEY,
22+
password=API_SECRET,
23+
account_sid=ACCOUNT_SID,
24+
credential_provider= ClientCredentialProvider(CLIENT_ID, CLIENT_SECRET)
25+
)
26+
msg = self.client.messages.create(
27+
to=self.to_number, from_=self.from_number, body="hello world"
28+
)
29+
self.assertEqual(msg.to, self.to_number)
30+
self.assertEqual(msg.from_, self.from_number)
31+
self.assertEqual(msg.body, "hello world")
32+
self.assertIsNotNone(msg.sid)
33+
34+
if __name__ == "__main__":
35+
example()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from twilio.http.orgs_token_manager import OrgTokenManager
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 = OrgTokenManager(
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