Wednesday, February 10, 2021

Azure/O365/Teams authentication and monitoring bash curl scripts

Authorize for teams.
Replace YOUR_TENANT_ID, YOUR_EMAIL and YOUR_PASSWORD.
Use one of these client_id’s, depending on your usecase.
1fec8e78-bce4-4aaf-ab1b-5451cc387264 (Teams mobile/desktop application)
5e3ce6c0-2b1f-4285-8d4b-75ee78787346 (Teams web application)

auth.sh:

#!/bin/bash

curl -s -X POST https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/token \
-c cookies.txt \
-o auth.blob \
-F grant_type=password \
-F resource=https://teams.microsoft.com/ \
-F client_id=1fec8e78-bce4-4aaf-ab1b-5451cc387264 \
-F username=YOUR_EMAIL \
-F password=YOUR_PASSWORD

This will save your bearer token, amongst others, to auth.blob in a json object.

Because the bearer token is only valid for a certain period of time, you’ll need to refresh it. Here’s how. You’ll need ‘jq’ installed to decompose the json object.
refresh.sh:

#!/bin/bash

REFRESHTOKEN=`cat auth.blob | jq ".refresh_token" | sed 's/"//g'`

curl -s -X POST https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/token \
-c cookies.txt \
-o auth.blob \
-F grant_type=refresh_token \
-F resource=https://teams.microsoft.com/ \
-F client_id=1fec8e78-bce4-4aaf-ab1b-5451cc387264 \
-F refresh_token=$REFRESHTOKEN

In the script you can keep repeating actions, but in order to keep your token active, you can use the following piece of code:

if [ -f "auth.blob" ]; then
  EXPIRES=`cat auth.blob | jq ".expires_on" | sed 's/"//g'`
  NOW=`date +%s`
  TTL=`expr $EXPIRES - $NOW`
  if [ $TTL -lt 60 ]; then
    echo "time for a refresh!"
    ./refresh.sh
  fi
else
  echo "no previous auth present!"
  ./auth.sh
  EXPIRES=`cat auth.blob | jq ".expires_on" | sed 's/"//g'`
  NOW=`date +%s`
  TTL=`expr $EXPIRES - $NOW`
fi

Now you can do the cool stuff like query your calendar or whatever:

#!/bin/bash

BEARER=`cat auth.blob | jq ".access_token" | sed 's/"//g'`
curl -s --write-out "%{http_code}|%{time_total}n" -o bla.txt "https://teams.microsoft.com/api/mt/emea/beta/me/calendarEvents?StartDate=2021-02-07T23:00:00.000Z&EndDate=2021-02-14T23:00:00.000Z" \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Teams/1.3.00.30866 Chrome/80.0.3987.165 Electron/8.5.1 Safari/537.36" \
-H "authorization: Bearer $BEARER"

Or verify your local timezone:

#!/bin/bash

BEARER=`cat auth.blob | jq ".access_token" | sed 's/"//g'`

date "+%Y.%m.%e %T %N"
curl -v 'https://teams.microsoft.com/api/mt/part/emea-03/beta/me/calendarEvents/timeZoneSettingsWithOffset?timezone=Europe%2FAmsterdam' \
-H "authorization: Bearer $BEARER" \
-H 'authority: teams.microsoft.com'
echo ""
date "+%Y.%m.%e %T %N"
  1. 按摩師證照班

    Wednesday, October 1, 2025 at 16:07:59

    https://storage.googleapis.com/digi477sa/research/digi477sa-(122).html
    If stylish and complex is what you’re going for, think
    about this sequined ensemble from Alex Evenings.

  2. 整復師

    at 18:57:47

    https://je-tall-marketing-872.ams3.digitaloceanspaces.com/research/je-marketing-(454).html
    Steer away from something too near white similar to champagne and beige colours without speaking to your daughter beforehand.

  3. 身體按摩課程

    at 19:36:21

    https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-735/research/je-marketing-(450).html
    Celebrate their huge day in style with our Mother of the
    Bride or Groom outfits.

  4. 按摩學徒

    at 20:04:40

    https://je-tall-marketing-872.ams3.digitaloceanspaces.com/research/je-marketing-(170).html
    I can guarantee you that I won’t ever make that mistake once more.

  5. 學按摩課程

    at 20:30:44

    https://storage.googleapis.com/digi462sa/research/digi462sa-(156).html
    Red Dress has some very beautiful and chic attire, and they’re inexpensive.

  6. 腳底按摩證照

    Thursday, October 2, 2025 at 02:35:16

    https://jekyll.s3.us-east-005.backblazeb2.com/20250909-5/research/je-marketing-(470).html
    Then you’ll view your saved listings each time you login.

  7. http://posuda86-wordpress-ugas8.tw1.ru/agent/meic6411826559/

    Thursday, October 23, 2025 at 01:06:11

    I’m not that much of a internet reader to be honest but your sites really nice,
    keep it up! I’ll go ahead and bookmark your website to come back later.
    Many thanks

  8. jam trading forex indonesia

    Sunday, November 30, 2025 at 18:19:17

    Simply want to say your article is as astounding.
    The clearness in your post is just nice and i can assume you
    are an expert on this subject. Well with your permission allow me to grab your feed to keep
    updated with forthcoming post. Thanks a million and please keep up the enjoyable work.

Add comment

Fill out the form below to add your own comments