Skip to content

Provide access to the Access Tokens API and enable initialization from OAuth Authentication Codes and Refresh Tokens #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions webexteamssdk/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,66 @@ def single_request_timeout(self):
def wait_on_rate_limit(self):
"""Automatic rate-limit handling enabled / disabled."""
return self._session.wait_on_rate_limit

# Create a class attribute for the Access Tokens API that can be accessed
# before WebexTeamsAPI object is initialized.
access_tokens = AccessTokensAPI(
base_url=DEFAULT_BASE_URL,
object_factory=immutable_data_factory,
single_request_timeout=DEFAULT_SINGLE_REQUEST_TIMEOUT,
)

@classmethod
def from_oauth_code(cls, client_id, client_secret, code, redirect_uri):
"""Create a new WebexTeamsAPI connection object using an OAuth code.

Exchange an Authorization Code for an Access Token, then use the access
token to create a new WebexTeamsAPI connection object.

Args:
client_id(basestring): Provided when you created your integration.
client_secret(basestring): Provided when you created your
integration.
code(basestring): The Authorization Code provided by the user
OAuth process.
redirect_uri(basestring): The redirect URI used in the user OAuth
process.

Returns:
WebexTeamsAPI: A new WebexTeamsAPI object initialized with the
access token from the OAuth Authentication Code exchange.

Raises:
TypeError: If the parameter types are incorrect.
ApiError: If the Webex Teams cloud returns an error.
"""
token_obj = cls.access_tokens.get(client_id, client_secret, code,
redirect_uri)

return cls(access_token=token_obj.access_token)

@classmethod
def from_oauth_refresh(cls, client_id, client_secret, refresh_token):
"""Create a new WebexTeamsAPI connection object using an OAuth refresh.

Exchange a refresh token for an Access Token, then use the access
token to create a new WebexTeamsAPI connection object.

Args:
client_id(basestring): Provided when you created your integration.
client_secret(basestring): Provided when you created your
integration.
refresh_token(basestring): Provided when you requested the Access
Token.

Returns:
WebexTeamsAPI: A new WebexTeamsAPI object initialized with the
access token from the OAuth Refresh Token exchange.

Raises:
TypeError: If the parameter types are incorrect.
ApiError: If the Webex Teams cloud returns an error.
"""
token_obj = cls.access_tokens.refresh(client_id, client_secret,
refresh_token)
return cls(access_token=token_obj.access_token)