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 it´s install the library with: install.packages(AzureGraph)
library(AzureGraph) library(AzureAuth) library(httr)
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 “https://graph.microsoft.com/” Tenant: The Tenant id. App: The Id of our app created in Azure. Password: The password of our App.
token <- get_azure_token("https://graph.microsoft.com/", tenant="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", app="xxxxxxxxx-xxxx-xxxx-xxxxxxxxx", password="xxxxxxxxxxxxxxxxxxxx") 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: https://docs.microsoft.com/en-us/graph/permissions-reference
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("https://graph.microsoft.com/v1.0/reports/getTeamsUserActivityUserDetail(period='D7')", add_headers(Authorization = paste0("Bearer ", token)), encode = "json", type = "basic") TeamsUserActivityUserDetailUrlResults <- as.character(TeamsUserActivityUserDetail) 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!