Skip to content

Commit

Permalink
[79420] Enable full payload response for exchanging the token for code (
Browse files Browse the repository at this point in the history
#190)

This PR introduces a new method, send_authorization that will call the /oauth/token endpoint and returns the full payload that includes the access token within it.
  • Loading branch information
mrashed-dev authored Jan 31, 2022
1 parent 45e7bda commit 5f7e0a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
nylas-python Changelog
======================

Unreleased
----------------
* Enable full payload response for exchanging the token for code

v5.4.2
----------------
* Add support for `Event` to ICS
Expand Down
17 changes: 15 additions & 2 deletions nylas/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,16 @@ def authentication_url(
url = URLObject(self.authorize_url).add_query_params(args.items())
return str(url)

def token_for_code(self, code):
def send_authorization(self, code):
"""
Exchanges an authorization code for an access token.
Args:
code (str): The authorization code returned from authenticating the user
Returns:
dict: The response from the API containing the access token
"""
args = {
"client_id": self.client_id,
"client_secret": self.client_secret,
Expand All @@ -186,7 +195,11 @@ def token_for_code(self, code):
self.access_token_url, data=urlencode(args), headers=headers
).json()

self.access_token = resp[u"access_token"]
self.access_token = resp["access_token"]
return resp

def token_for_code(self, code):
self.send_authorization(code)
return self.access_token

def is_opensource_api(self):
Expand Down
2 changes: 1 addition & 1 deletion nylas/client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NylasApiError(HTTPError):
def __init__(self, response):
try:
response_json = json.loads(response.text)
error_message = u"%s %s. Reason: %s. Nylas Error Type: %s" % (
error_message = "%s %s. Reason: %s. Nylas Error Type: %s" % (
response.status_code,
response.reason,
response_json["message"],
Expand Down

0 comments on commit 5f7e0a5

Please sign in to comment.