Skip to content

Microsoft Graph

Angel Sanadinov edited this page Jun 27, 2017 · 3 revisions

core3 has basic support for making requests to the Microsoft Graph API via MicrosoftGraphConnection and its corresponding ServiceConnectionComponent.

Links

Initialization

import akka.actor.ActorSystem
import akka.util.Timeout
import com.google.inject.{AbstractModule, Provides, Singleton}
import core3.config.StaticConfig
import core3.http.requests.{MicrosoftGraphConnection, msgraph}
import net.codingwell.scalaguice.ScalaModule
import play.api.libs.ws.WSClient

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._


class Module extends AbstractModule with ScalaModule {

    //...

    @Provides
    @Singleton
    def provideGraphConnection(
      ws: WSClient,
      system: ActorSystem
    )(implicit ec: ExecutionContext): MicrosoftGraphConnection = {
      implicit val timeout = Timeout(15.seconds)
    
      new MicrosoftGraphConnection(
        system.actorOf(
          msgraph.ServiceConnectionComponent.props(
            ws,
            StaticConfig.get.getConfig(
              "security.authentication.services.<SOME_MICROSOFT_GRAPH_SERVICE_NAME>"
            )
          )
        )
      )
    }
    
    //...

}

Use

val connection: MicrosoftGraphConnection = ...

//https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list
val (resultCode, users): Future[(Int, JsValue)] =
  connection.get("/users")

val someUserID = ...
val someEventData = Json.obj(
  ...
)

//https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_post_events
val (resultCode, createResult): Future[(Int, JsValue)] =
  connection.post(s"/users/$someUserID/events", Some(someEventData))

Application Setup

  1. Register application - Microsoft App Registration Portal
  2. Add application secret (Generate New Password)
  3. Add application permissions (user delegation is not supported)
  4. Update any other settings as needed
  5. Give admin consent via Azure AD (may also be possible via Microsoft Azure Portal)
  6. Done

Configuration

Only tenantID, clientId and clientSecret should be needed. All other options can be kept as they are.

server.static {
  security {
    authentication {
      services {
        SOME_MICROSOFT_GRAPH_SERVICE_NAME {
          uri = "https://graph.microsoft.com"
          authProvider = "https://login.microsoftonline.com"
          tenantId = "<some tenant GUID or friendly name>"
          clientId = "<some application ID>"
          clientSecret = "<some client secret>"
          scope = ".default"
          jwksUri = "https://login.windows.net/common/discovery/keys"
        }
      }
    }
  }
}
Parameter Found In
tenantId Microsoft Azure Portal -> Azure Active Directory -> Properties -> Directory ID
clientId Microsoft App Registration Portal -> App ID / Client ID
clientSecret Microsoft App Registration Portal -> select application -> Generate New Password

Clone this wiki locally