GithubHelp home page GithubHelp logo

Comments (6)

jasonjoh avatar jasonjoh commented on June 10, 2024

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.

from msgraph-sample-pythondjangoapp.

barry-scott avatar barry-scott commented on June 10, 2024

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

from msgraph-sample-pythondjangoapp.

jasonjoh avatar jasonjoh commented on June 10, 2024

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

  • msal to manage authentication and token acquisition.
  • requests for making HTTP requests to the Graph API endpoint.

App registration

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

  • Redirect URI: Public client/native (mobile & desktop), http://localhost

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'))

from msgraph-sample-pythondjangoapp.

 avatar commented on June 10, 2024

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.

from msgraph-sample-pythondjangoapp.

barry-scott avatar barry-scott commented on June 10, 2024

Thanks I will test that example shortly.

from msgraph-sample-pythondjangoapp.

 avatar commented on June 10, 2024

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.

from msgraph-sample-pythondjangoapp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.