What Microsoft knows about you and how it can be used to gain useful knowledge

Microsoft. , , , β€” Microsoft. , Microsoft Cloud. Microsoft β€” API-, , ?





Dashboard API Microsoft Graph Django OneDrive, Outlook .






, , . :





  • API, , ;





  • , , ;





  • , Django.





A simple Dashboard application in Django.
Dashboard Django.

  • Microsoft Graph β€” API Microsoft Graph.





  • OAuth 2.0 β€” .





  • Graph API.





  • Plotly.





  • .





Microsoft Graph β€” API Microsoft Graph

Microsoft Live SDK Microsoft Graph. API Microsoft 365 Microsoft Graph [1, 2].





API Microsoft Graph . β€” Microsoft Graph, App ID App Secret , .





(1) , . Python, Django.





Select a programming language.
.

Django, , Django. , Microsoft Docs.





(2) App ID App Secret β€” .





Azure Azure Active Directory.





Azure portal.
Azure.

(3) , Microsoft Graph, . .





Django sample code.
Django.

  Django. oauth_settings.yml β€” , App ID App Secret.





Project structure.
.





(4) Python.





requirements.txt [3]. , [4]. : virtualenv_graph. .





python -m venv virtualenv_graph
      
      



virtualenv_graph Python. graph-tutorial , activate:





graph-tutorial\Scripts\activate.bat
      
      



( ). , . Python :





Migrate β€”





(5) :





python manage.py migrate
      
      



Django: django.contrib.auth django.contrib.session.





Django default tables.
Django .

(6) python manage.py runserver - . , - 8000 127.0.0.1.





The start page of a typical application.
.

, Office365.





OAuth 2.0 β€”

, , , OAuth 2.0.OAuth 2.0 β€” , [5, 6].





, OAuth . ? , OAuth [5].





  • : , . Microsoft Cloud Microsoft 365 , .





  • : , .





  • : , , β€” Django.





  • : ( ) . .





    OAuth 2.0 for web server applications.
    OAuth 2.0 - .

, Python Template.





Microsoft 

, . OAuth 2.0. Microsoft [7].





Sign-in sign_in tutorial.views.py, URL-, , get_sign_in_url. /authorize. [7]:





  • ID , ;





  • scope, , ;





  • URI β€” , .





, , YAML- oauth_settings.yml.





OAuth 2.0 workflow in Django.
OAuth 2.0 Django.

(3) , , , URI .





(4) App ID App Secret, .





(5) API , scope.





OAuth 2.0 workflow in Django.
OAuth 2.0 Django.

, . , .





Graph API

Graph. , Microsoft. , , (, OneDrive).





, , , One Drive. SharePoint, OneNote, Outlook, Teams .





scope oauth_settings.yml . : Microsoft Graph. One Drive, scope Files.ReadWrite.All. : /Bilder/Eigene Aufnahmen. GET /drive/root:/<path>:/children .





# <graph_helper.py>
def get_one_drive_items(token):
  graph_client = OAuth2Session(token=token)

  # Send GET to /drive/root
  items = graph_client.get('{0}/drive/root:/Bilder/Eigene Aufnahmen:/children'.format(graph_url))

  # Return the JSON result
  return items.json()
  
# <views.py>
def one_drive(request):
    context = initialize_context(request)

    token = get_token(request)

    files = get_one_drive_items(token)

    for file in files['value']:
        file['DownloadUrl'] = file['@microsoft.graph.downloadUrl']

    context['files'] = files['value']

    return render(request, 'tutorial/one_drive.html', context)
      
      



, one_drive, β€” HTML- one_drive.html API. one_drive.html calendar.html, .





{% extends "tutorial/layout.html" %}
{% block content %}
<h1>Pictures uploaded to One Drive</h1>
<table class="table">
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Created</th>
      <th scope="col">Download Link</th>
    </tr>
  </thead>
  <tbody>
    {% if files %}
      {% for file in files %}
        <tr>
          <td>{{ file.name }}</td>
          <td>{{ file.createdDateTime }}</td>
          <td><a href="{{ file.DownloadUrl }}">Download</a></td>
        </tr>
      {% endfor %}
    {% endif %}
  </tbody>
</table>
{% endblock %}
      
      



one_drive urls.py.





# <tutorial.urls.py>
from django.urls import path
from . import views

urlpatterns = [
  ...
  path('calendar', views.calendar, name='calendar'),
  path('one_drive', views.one_drive, name='one_drive'),
]
      
      



http://localhost:8000/one_drive HTML- /Bilder/Eigene Aufnahme.





Plotly.

Plotly. , Plotly, pip:





pip install plotly
      
      



, . , , .





Β« β€” Β» files['values'], . Pandas. Pandas , . groupby CreationWeek, .count() .





def one_drive(request):
    context = initialize_context(request)

    token = get_token(request)

    files = get_one_drive_items(token)

    for file in files['value']:
        file['DownloadUrl'] = file['@microsoft.graph.downloadUrl']
        file['CreationDay'] = datetime.datetime.strptime(file['createdDateTime'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
        file['CreationWeek'] = str(file['CreationDay'].isocalendar()[1]) + '-' + str(file['CreationDay'].year)

    # sum all items by calendar week
    items_df = pd.DataFrame.from_dict(files['value'])
    items_df = items_df.groupby(by="CreationWeek", dropna=False).count()
    items_df.rename(columns={'createdDateTime': 'count'}, inplace=True)

    # define new figure with plotly
    fig = px.bar(x=items_df.index.tolist(),
                     y=items_df['count'].tolist(),
                     labels=dict(x="Calendar Week", y="Number of umploaded pictures")
                     )

    # graph plot
    plt_div = plot(fig, output_type='div')

    # define context
    context['files'] = files['value']
    context['plot_div'] = plt_div

    return render(request, 'tutorial/one_drive.html', context)
      
      



. (), Microsoft.





Data visualization using Plotly.
Plotly.





API- Microsoft Graph. , :





1) Microsoft;





2) Graph API;





3) Plotly.





Graph POST. , , Microsoft Team β€” HTTP-. , .





  1. Microsoft Graph





  2. Live SDK Microsoft Graph





  3. Pip









  4. OAuth 2.0





  5. OAuth 2.0





  6. Microsoft OAuth 2.0





  7. API -





, :





  • Data Scientist





  • Data Analyst





  • Data Engineering









  • Fullstack- Python





  • Java-





  • QA- JAVA





  • Frontend-









  • C++





  • Unity





  • -





  • iOS-





  • Android-









  • Machine Learning





  • "Machine Learning Deep Learning"





  • " Data Science"





  • " Machine Learning Data Science" 





  • "Python -"





  • " "









  • DevOps








All Articles