Skip to content

Commit

Permalink
Examples updates, ClientContext.with_interactive method introduced, r…
Browse files Browse the repository at this point in the history
…efactor auth
  • Loading branch information
vgrem committed Jul 6, 2023
1 parent f109403 commit a702fbf
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 64 deletions.
6 changes: 4 additions & 2 deletions examples/auth/interactive_sharepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
For example, to sign in, perform multi-factor authentication (MFA), or to grant consent
to more resource access permissions.
Note:
in AAD portal ensure Mobile and Desktop application is added and http://localhost is set as redirect uri
Prerequisite: In Azure Portal, configure the Redirect URI of your
"Mobile and Desktop application" as ``http://localhost``.
https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#interactive-and-non-interactive-authentication
"""
Expand All @@ -28,4 +28,6 @@ def acquire_token():

ctx = ClientContext(test_site_url).with_access_token(acquire_token)
me = ctx.web.current_user.get().execute_query()
web = ctx.web.get().execute_query()
print(me.login_name)
print(web.title)
3 changes: 1 addition & 2 deletions examples/onedrive/drives/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
https://learn.microsoft.com/en-us/graph/api/drive-list?view=graph-rest-1.0&tabs=http
"""
from examples import acquire_token_by_client_credentials
from office365.graph_client import GraphClient
from office365.onedrive.drives.drive import Drive

from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
drives = client.drives.get().top(10).execute_query()
Expand Down
15 changes: 8 additions & 7 deletions examples/onedrive/files/export.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# --------------------------------------------------------------------------
# Example demonstrates how to export OneDrive files into local file system
# --------------------------------------------------------------------------
"""
Example demonstrates how to download OneDrive files into local file system
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get_content?view=odsp-graph-online
"""

import os
import tempfile

from examples import acquire_token_by_client_credentials, sample_user_principal_name
from office365.graph_client import GraphClient
from office365.onedrive.drives.drive import Drive
from tests import test_user_principal_name
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)


drive = client.users[sample_user_principal_name].drive # type: Drive
drive = client.users[test_user_principal_name].drive # type: Drive
with tempfile.TemporaryDirectory() as local_path:
drive_items = drive.root.children.get().execute_query()
file_items = [item for item in drive_items if item.file is not None] # files only
Expand Down
2 changes: 1 addition & 1 deletion examples/onedrive/folders/download_files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import tempfile

from examples import acquire_token_by_username_password
from office365.graph_client import GraphClient
from office365.onedrive.driveitems.driveItem import DriveItem
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
# address folder by path
Expand Down
8 changes: 5 additions & 3 deletions examples/onedrive/powerpoint/create.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from examples import acquire_token_by_username_password
from office365.graph_client import GraphClient
"""
Creates a PowerPoint file
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)

remote_drive = client.me.drive.root
pptx_file = remote_drive.create_powerpoint("sample.pptx").execute_query()
print(f"File {pptx_file.web_url} has been uploaded")
6 changes: 4 additions & 2 deletions examples/onedrive/sites/list_sites.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""
Get a collection of sites.
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/site_list_subsites?view=odsp-graph-online
"""
from examples import acquire_token_by_client_credentials

from office365.graph_client import GraphClient
from office365.onedrive.sites.site import Site

from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
sites = client.sites.paged(100).get().execute_query()
Expand Down
6 changes: 5 additions & 1 deletion examples/onedrive/termstore/get_term_set.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from examples import acquire_token_by_username_password
"""
Get term set by name
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
term_store = client.sites.root.term_store
Expand Down
19 changes: 19 additions & 0 deletions examples/sharepoint/connect_interactive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Demonstrates how to login when the user may be prompted for input by the authorization server.
For example, to sign in, perform multi-factor authentication (MFA), or to grant consent
to more resource access permissions.
Prerequisite: In Azure Portal, configure the Redirect URI of your
"Mobile and Desktop application" as ``http://localhost``.
https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#interactive-and-non-interactive-authentication
"""

from office365.sharepoint.client_context import ClientContext
from tests import test_tenant, test_client_id, test_site_url

ctx = ClientContext(test_site_url).with_interactive(test_tenant, test_client_id)
me = ctx.web.current_user.get().execute_query()
print(me.login_name)
web = ctx.web.get().execute_query()
print(web.title)
1 change: 1 addition & 0 deletions examples/sharepoint/connect_with_client_certificate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Example: Azure AD App-Only auth flow
https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread
"""

Expand Down
2 changes: 1 addition & 1 deletion examples/teams/list_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
https://learn.microsoft.com/en-us/graph/teams-list-all-teams?context=graph%2Fapi%2F1.0&view=graph-rest-1.0
"""

from examples import acquire_token_by_client_credentials
from office365.graph_client import GraphClient
from office365.teams.team import Team
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
teams = client.teams.get_all().select(["displayName"]).execute_query()
Expand Down
4 changes: 2 additions & 2 deletions generator/import_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def export_to_file(path, content):

parser = ArgumentParser()
parser.add_argument("-e", "--endpoint", dest="endpoint",
help="Import metadata endpoint", default="microsoftgraph")
help="Import metadata endpoint", default="sharepoint")
parser.add_argument("-p", "--path",
dest="path", default="./metadata/MicrosoftGraph.xml",
dest="path", default="./metadata/SharePoint.xml",
help="Import metadata endpoint")

args = parser.parse_args()
Expand Down
Loading

0 comments on commit a702fbf

Please sign in to comment.