Getting data from Microsoft Azure with R

Hi, how is it going?

Today I want to show you how to take data from Microsoft Azure with R and Rest API method.

We have two options to do that, we can use the API rest of Microsfot or we can use this library for R : AzureGraph.

This library is very usefull because allow us to access data in various online Microsoft services.

So the fisrt think that we must do its install the library with: install.packages(AzureGraph)


One time we have the library ready we must to proceed to login in our Microsoft account, Note, we must be sure that we have the permiss necesaries to access to the differents Services in Microsoft Suite.

We can get the token in two ways:

1 Login for Users :

We can call ti the function create_graph_login(), this functions returns the token inside the enviroment login, A pop-up window will open for you to give permissions to the application.

login <- create_graph_login()
token <- login$token$credentials$access_token

2 Login for Apps :

The other way is with the function get_azure_token(), This method is recommended when we want to give permissions to an application and not to a user, for example when we want to create a script that launches automatically.

To call get_azure_token() we will need to know:

Resource: In this case is Tenant: The Tenant id. App: The Id of our app created in Azure. Password: The password of our App.

 token <- get_azure_token("", tenant="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", app="xxxxxxxxx-xxxx-xxxx-xxxxxxxxx",
 token <- token$credentials$access_token

Once we have the token we can access the different Microsoft applications,remember that you have to take into account that you have to have the appropiate permissions to be able to access, if you want more information about Microsoft permissions you can visit:

And now we are ready to work with Azre Graph in R, here I give you and example of How to get for example all our users in Microsfot Teams for the last 7 days.

TeamsUserActivityUserDetail <-  GET("'D7')",
                                    add_headers(Authorization = paste0("Bearer ", token)),
                                    encode = "json", type = "basic")
TeamsUserActivityUserDetailUrlResults <- as.character(TeamsUserActivityUserDetail[1])
TeamsData <-read.csv(TeamsUserActivityUserDetailUrlResults, encoding = "utf8")
## # A tibble: 5 x 13
##      X1 Report.Refresh.~ User.Principal.~ Last.Activity.D~ Is.Deleted
##   <dbl> <date>           <chr>            <date>           <lgl>     
## 1     1 2020-04-03       User_1           2019-10-04       FALSE     
## 2     2 2020-04-03       User_2           NA               FALSE     
## 3     3 2020-04-03       User_3           NA               FALSE     
## 4     4 2020-04-03       User_4           NA               FALSE     
## 5     5 2020-04-03       User_5           2019-10-07       FALSE     
## # ... with 8 more variables: Deleted.Date <lgl>, Assigned.Products <chr>,
## #   Team.Chat.Message.Count <dbl>, Private.Chat.Message.Count <dbl>,
## #   Call.Count <dbl>, Meeting.Count <dbl>, Has.Other.Action <chr>,
## #   Report.Period <dbl>

This has been all for today, if you have any questions you can leave it in the comments and I will reply as soon as possible!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *