microsoftgraph / msgraph-sample-pythondjangoapp

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Python web apps.
MIT License
132 stars 63 forks source link

OWA graph simple example required - no django #58

Closed barry-scott closed 3 years ago

barry-scott commented 3 years ago

I am researching how to add calendar support to one of my projects.

This tutorial is asking that I know django before I can untangle the logic of talking to graph. I do not know django so that is making this tutorial unhelpful.

Do you have a tutorial that is a simple command line that just shows the use of the API?

jasonjoh commented 3 years ago

Thanks @barry-scott for your feedback. What sort of project are you trying to incorporate with? We do not have a command-line based tutorial for Python currently. Command line apps would use a different auth flow which may nor may not be applicable to your scenario. If you can share some details on your project I may be able to at least point you in the right direction.

barry-scott commented 3 years ago

I am working on a PyQt5 app that runs on a touch screen.

What I need is an example that shows how to get calendar events from OWA. How do I setup the app key? How do I query events?

I do not need to be shown how to build those details in to my app.

If you look at google's calendard API example for the sort of thing I was hoping to find for graph API: https://developers.google.com/calendar/quickstart/python

jasonjoh commented 3 years ago

Ok. It's not going to be all that different from this Django-based sample. You'll use the same packages, but the auth flow will be different since you're on a mobile device. The API calls are exactly the same.

Dependencies

App registration

Register an app at https://aad.portal.azure.com.

Copy your Application (client) ID from the overview page. If you chose Accounts in this organizational directory only when creating the registration, you'll also need to copy your Directory (tenant) ID.

Code

import requests
from msal import PublicClientApplication

# Register your app in Azure AD to get an application ID
client_id = 'YOUR_APP_ID_HERE'
# If you register your app for any organization, leave as common
# If you register it for only users in your organization, change to
# your tenant ID
tenant_id = 'common'
# Array of Microsoft Graph scopes you need
scopes = [ 'User.Read', 'Calendars.Read' ]

if __name__ == '__main__':
  # Create MSAL public client
  app = PublicClientApplication(
    client_id,
    authority = 'https://login.microsoftonline.com/{0}'.format(tenant_id))

  result = None
  # Check for accounts in cache
  accounts = app.get_accounts()
  if accounts:
    # For simplicity use the first account
    user_account = accounts[0]
    # Acquire the token silently
    result = app.acquire_token_silent(scopes, user_account)

  if not result:
    # No accounts in the cache, get a token interactively
    result = app.acquire_token_interactive(scopes)

  if 'access_token' in result:
    access_token = result['access_token']

    user_response = requests.get(
      'https://graph.microsoft.com/v1.0/me',
      headers = {
        'Authorization': 'Bearer {0}'.format(access_token)
      }
    )

    user = user_response.json()
    if 'displayName' in user:
      print('Hello {0}'.format(user['displayName']))
    else:
      print(user)

    calendar_response = requests.get(
      'https://graph.microsoft.com/v1.0/me/calendarView',
      headers = {
        'Authorization': 'Bearer {0}'.format(access_token)
      },
      params = {
        'startDateTime': '2021-05-03T00:00:00Z',
        'endDateTime': '2021-05-10T00:00:00Z',
        '$select': 'subject, start, end'
      }
    )

    print(calendar_response.json())

  else:
    print(result.get('error'))
    print(result.get('error_description'))
    print(result.get('correlation_id'))
ghost commented 3 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

barry-scott commented 3 years ago

Thanks I will test that example shortly.

ghost commented 3 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.