Skip to content

Commit d97964e

Browse files
author
Alejandro Casanovas
committed
Fix googleapis#187. Fixed default redirect uri used by this package.
Fixed Account.is_authenticated property was returning True/None instead of True/False. Updated Readme to change the app registration portal to azure, and to show the usage of the new default redirect uri. Bumped version
1 parent 91dcb66 commit d97964e

4 files changed

Lines changed: 23 additions & 18 deletions

File tree

O365/account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def is_authenticated(self):
5353
if not token:
5454
token = self.con.token_backend.get_token()
5555

56-
return token and not token.is_expired
56+
return token is not None and not token.is_expired
5757

5858
def authenticate(self, *, scopes, **kwargs):
5959
""" Performs the oauth authentication flow resulting in a stored token

O365/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
O365_API_VERSION = 'v2.0'
2525
GRAPH_API_VERSION = 'v1.0'
26-
OAUTH_REDIRECT_URL = 'https://outlook.office365.com/owa/'
26+
OAUTH_REDIRECT_URL = 'https://login.microsoftonline.com/common/oauth2/nativeclient' # version <= 1.1.3. : 'https://outlook.office365.com/owa/'
2727

2828
RETRIES_STATUS_LIST = (
2929
429, # Status code for TooManyRequests

README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,25 @@ The `Connection` Class handles the authentication.
117117
This section is explained using Microsoft Graph Protocol, almost the same applies to the Office 365 REST API.
118118

119119
##### Authentication Flow
120-
1. To work with oauth you first need to register your application at [Microsoft Application Registration Portal](https://apps.dev.microsoft.com/).
121-
122-
1. Login at [Microsoft Application Registration Portal](https://apps.dev.microsoft.com/)
123-
2. Create an app, note your app id (client_id)
124-
3. Generate a new password (client_secret) under "Application Secrets" section
125-
4. Under the "Platform" section, add a new Web platform and set "https://outlook.office365.com/owa/" as the redirect URL
126-
5. Under "Microsoft Graph Permissions" section, add the delegated permissions you want (see scopes), as an example, to read and send emails use:
120+
1. To work with oauth you first need to register your application at [Azure App Registrations](https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade).
121+
122+
1. Login at [Azure Portal (App Registrations)](https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade)
123+
1. Create an app. Set a name.
124+
1. In Supported account types choose "Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com)", if you are using a personal account.
125+
1. Set the redirect uri (Web) to: `https://login.microsoftonline.com/common/oauth2/nativeclient` and click register. This is the default redirect uri used by this library, but you can use any other if you want.
126+
1. Write down the Application (client) ID. You will need this value.
127+
1. Under "Certificates & secrets", generate a new client secret. Set the expiration preferably to never.
128+
1. Write down the value of the client secret created now. It will be hidden later on.
129+
1. Under Api Permissions add the delegated permissions for Microsoft Graph you want (see scopes), as an example, to read and send emails use:
127130
1. Mail.ReadWrite
128-
2. Mail.Send
129-
3. User.Read
130-
4. It is highly recommended to add "offline_access" permission. If not you will have to re-authenticate every hour.
131+
1. Mail.Send
132+
1. User.Read
133+
1. It is highly recommended to add "offline_access" permission. If not you will have to re-authenticate every hour.
131134

132-
2. Then you need to login for the first time to get the access token by consenting the application to access the resources it needs.
135+
1. Then you need to login for the first time to get the access token by consenting the application to access the resources it needs.
133136
1. To authenticate (login) call `account.authenticate` and pass the scopes you want (the ones you previously added on the app registration portal).
134137

135-
You can pass "protocol scopes" (like: "https://graph.microsoft.com/Calendars.ReadWrite") to the method or use "[scope helpers](https://github.com/O365/python-o365/blob/master/O365/connection.py#L33)" like ("message_all").
138+
You can pass "protocol scopes" (like: "https://graph.microsoft.com/Calendars.ReadWrite") to the method or use "[scope helpers](https://github.com/O365/python-o365/blob/master/O365/connection.py#L34)" like ("message_all").
136139
If you pass protocol scopes, then the `account` instance must be initialized with the same protocol used by the scopes. By using scope helpers you can abstract the protocol from the scopes and let this library work for you.
137140
Finally, you can mix and match "protocol scopes" with "scope helpers".
138141
Go to the [procotol section](#protocols) to know more about them.
@@ -148,14 +151,14 @@ This section is explained using Microsoft Graph Protocol, almost the same applie
148151
```
149152
This method call will print a url that the user must visit to give consent to the app on the required permissions.
150153

151-
The user must then visit this url and give consent to the application. When consent is given, the page will rediret to: "https://outlook.office365.com/owa/" by default (you can change this).
154+
The user must then visit this url and give consent to the application. When consent is given, the page will rediret to: "https://login.microsoftonline.com/common/oauth2/nativeclient" by default (you can change this) with a url query param called 'code'.
152155

153156
Then the user must copy the resulting page url and paste it back on the console.
154-
The method will the return True if the login attempt was succesful.
157+
The method will then return True if the login attempt was succesful.
155158

156159
**Take care: the access (and refresh) token must remain protected from unauthorized users.**
157160

158-
3. At this point you will have an access token stored that will provide valid credentials when using the api. If you change the scope requested, then the current token won't work, and you will need the user to give consent again on the application to gain access to the new scopes requested.
161+
1. At this point you will have an access token stored that will provide valid credentials when using the api. If you change the scope requested, then the current token won't work, and you will need the user to give consent again on the application to gain access to the new scopes requested.
159162

160163
The access token only lasts **60 minutes**, but the app will automatically request new access tokens through the refresh tokens (if and only if you added the "offline_access" permission), but note that a refresh token only lasts for 90 days. So you must use it before or you will need to request a new access token again (no new consent needed by the user, just a login).
161164

@@ -628,6 +631,8 @@ for event in birthdays:
628631
event.decline("No way I'm comming, I'll be in Spain", send_response=False) # decline the event but don't send a reponse to the organizer
629632
```
630633

634+
There are some known issues when working with [shared calendars](https://docs.microsoft.com/en-us/graph/known-issues#calendars) in Microsoft Graph.
635+
631636
## OneDrive
632637
The `Storage` class handles all functionality around One Drive and Document Library Storage in Sharepoint.
633638

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup, find_packages
44

55

6-
VERSION = '1.1.3'
6+
VERSION = '1.1.4'
77

88
# Available classifiers: https://pypi.org/pypi?%3Aaction=list_classifiers
99
CLASSIFIERS = [

0 commit comments

Comments
 (0)