Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assignees: ''

- [ ] Downloaded from GitHub
- [ ] Downloaded from the [Microsoft Graph quick start tool](https://developer.microsoft.com/graph/quick-start)
- [ ] Followed the tutorial from [Microsoft Graph tutorials](https://docs.microsoft.com/graph/tutorials)
- [ ] Followed the tutorial from [Microsoft Graph tutorials](https://learn.microsoft.com/graph/tutorials)

### Describe the bug

Expand Down
6 changes: 5 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
version: 2
updates:
- package-ecosystem: "gradle" # See documentation for possible values
directory: "/demo/graphtutorial/" # Location of package manifests
directory: "/user-auth/graphtutorial/" # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "gradle" # See documentation for possible values
directory: "/app-auth/graphapponlytutorial/" # Location of package manifests
schedule:
interval: "weekly"
18 changes: 11 additions & 7 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ permissions:

jobs:
build:
defaults:
run:
working-directory: demo/graphtutorial

runs-on: ubuntu-latest

Expand All @@ -31,10 +28,17 @@ jobs:
with:
java-version: '17'
distribution: 'temurin'
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build with Gradle
- name: Make user app gradlew executable
run: chmod +x ./user-auth/graphtutorial/gradlew
- name: Build user app with Gradle
uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
with:
build-root-directory: demo/graphtutorial
build-root-directory: user-auth/graphtutorial
arguments: build
- name: Make app-only app gradlew executable
run: chmod +x ./app-auth/graphapponlytutorial/gradlew
- name: Build app-only app with Gradle
uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
with:
build-root-directory: app-auth/graphapponlytutorial
arguments: build
17 changes: 7 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ Thank you for contributing to this project! Before submitting your pull request,

## Overview

The code in this repository serves three purposes:
The code in this repository serves two purposes:

- The Markdown files in the [tutorial](/tutorial) folder are published as a tutorial on the [Microsoft Graph tutorials](https://docs.microsoft.com/graph/tutorials) page.
- The sample project in the [demo](/demo) folder is the source for a [Microsoft Graph quick start](https://developer.microsoft.com/graph/quick-start).**\***
- The sample project in the demo folder is also downloadable directly from GitHub and should run as-is after some simple configuration.
- The sample project in the [user-auth](/user-auth) folder is the source for a [Microsoft Graph quick start](https://developer.microsoft.com/graph/quick-start).
- The code in the [user-auth](/user-auth) and [app-auth](/app-auth) folders are directly referenced by [Microsoft Graph tutorials](https://learn.microsoft.com/graph/tutorials)

> **\*** Not all training repositories are available as quick starts (yet).

This is important to keep in mind, because changes in one place *may* require changes in another, to keep things in sync. Whereever possible, the Markdown files refer to the source code files directly (using a custom `:::code` syntax), so that updating code in source will automatically update the code in Markdown.
This is important to keep in mind, because changes in one place *may* require changes in another, to keep things in sync. The Markdown files for the tutorials refer to the source code files directly (using a custom `:::code` syntax), so that updating code in source will automatically update the code in Markdown.

## Updating code

Expand All @@ -24,19 +21,19 @@ Console.WriteLine("Hello World!");
// </MySnippet>
```

If you update code between these "marker" comments, the Markdown files will automatically get those changes when published to the Microsoft Graph documentation site. If you update code outside of those comments, it's very likely that you'll need to update the corresponding Markdown.
If you update code between these "marker" comments, the Markdown files will automatically get those changes when published to the Microsoft Graph documentation site. If you update code outside of those comments, it's very likely that you'll need to update the corresponding Markdown in the [Microsoft Graph training repository](https://github.com/microsoftgraph/microsoft-graph-training).

## Adding features

While the enthusiasm is appreciated, please don't send pull requests to add new features to the sample. Because this repository is primarily a "build your first app" tutorial, the feature set is limited, by design.

## Submitting pull requests

Please submit all pull requests to the `master` branch.
Please submit all pull requests to the `main` branch.

## When do changes get published?

Publishing of updates to the [Microsoft Graph tutorials](https://docs.microsoft.com/graph/tutorials) site is not automatic. Changes must first be promoted to the `live` branch, then a build must be triggered by the site admins. This is typically done on an "as-needed" basis.
Publishing of updates to the [Microsoft Graph tutorials](https://learn.microsoft.com/graph/tutorials) site is not automatic. Changes must first be promoted to the `live` branch, then a build must be triggered by the site admins. This is typically done on an "as-needed" basis.

## Code of conduct

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Microsoft Graph Training Module - Build Java apps with the Microsoft Graph Java SDK
# Build Java apps with the Microsoft Graph Java SDK - Completed project

This module will introduce you to working with the Microsoft Graph SDK to access data in Office 365 by building Java applications.
[![Java CI with Gradle](https://github.com/microsoftgraph/msgraph-training-java/actions/workflows/gradle.yml/badge.svg)](https://github.com/microsoftgraph/msgraph-training-java/actions/workflows/gradle.yml) ![License.](https://img.shields.io/badge/license-MIT-green.svg)

## Lab - Build Java apps with the Microsoft Graph Java SDK
This sample will introduce you to working with the Microsoft Graph SDK to access data in Microsoft 365 from Java applications. This code is the result of completing the [Java Microsoft Graph tutorial](https://learn.microsoft.com/graph/tutorials/java) and the [Java Microsoft Graph app-only tutorial](https://learn.microsoft.com/graph/tutorials/java-app-only).

In this lab you will create a console application using the Microsoft Authentication Library (MSAL) to access data in Office 365 using the Microsoft Graph.
## Running the sample

- [Java Microsoft Graph tutorial](https://docs.microsoft.com/graph/tutorials/java)
The code for the delegated user authentication sample is in the [user-auth](user-auth) folder. Instructions to configure and run the sample can be found in the [README](user-auth/README.md) in that folder.

## Completed sample
The code for the app-only authentication sample is in the [app-auth](app-auth) folder. Instructions to configure and run the sample can be found in the [README](app-auth/README.md) in that folder.

If you just want the completed sample generated by following this lab, you can find it here.
## Code of conduct

- [Completed project](demo)
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

## Disclaimer

Expand Down
84 changes: 84 additions & 0 deletions app-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# How to run the completed project

## Prerequisites

To run the completed project in this folder, you need the following:

- [Java SE Development Kit (JDK)](https://java.com/en/download/faq/develop.xml) and [Gradle](https://gradle.org/) installed on your development machine. (**Note:** This tutorial was written with OpenJDK version 17.0.2 and Gradle 7.4.2. The steps in this guide may work with other versions, but that has not been tested.)
- A Microsoft work or school account with the **Global administrator** role.

If you don't have a Microsoft account, you can [sign up for the Microsoft 365 Developer Program](https://developer.microsoft.com/microsoft-365/dev-program) to get a free Microsoft 365 subscription.

## Register an application

You can register an application using the Azure Active Directory admin center, or by using the [Microsoft Graph PowerShell SDK](https://learn.microsoft.com/graph/powershell/get-started).

### Azure Active Directory admin center

1. Open a browser and navigate to the [Azure Active Directory admin center](https://aad.portal.azure.com) and login using a Global administrator account.

1. Select **Azure Active Directory** in the left-hand navigation, then select **App registrations** under **Manage**.

1. Select **New registration**. Enter a name for your application, for example, `Java App-Only Graph Tutorial`.

1. Set **Supported account types** to **Accounts in this organizational directory only**.

1. Leave **Redirect URI** empty.

1. Select **Register**. On the application's **Overview** page, copy the value of the **Application (client) ID** and **Directory (tenant) ID** and save them, you will need these values in the next step.

1. Select **API permissions** under **Manage**.

1. Remove the default **User.Read** permission under **Configured permissions** by selecting the ellipses (**...**) in its row and selecting **Remove permission**.

1. Select **Add a permission**, then **Microsoft Graph**.

1. Select **Application permissions**.

1. Select **User.Read.All**, then select **Add permissions**.

1. Select **Grant admin consent for...**, then select **Yes** to provide admin consent for the selected permission.

1. Select **Certificates and secrets** under **Manage**, then select **New client secret**.

1. Enter a description, choose a duration, and select **Add**.

1. Copy the secret from the **Value** column, you will need it in the next steps.

### PowerShell

To use PowerShell, you'll need the Microsoft Graph PowerShell SDK. If you do not have it, see [Install the Microsoft Graph PowerShell SDK](https://learn.microsoft.com/graph/powershell/installation) for installation instructions.

1. Open PowerShell and run the [RegisterAppForAppOnlyAuth.ps1](RegisterAppForAppOnlyAuth.ps1) file with the following command.

```powershell
.\RegisterAppForAppOnlyAuth.ps1 -AppName "Java App-Only Graph Tutorial" -GraphScopes "User.Read.All"
```

1. Copy the **Client ID**, **Tenant ID**, and **Client secret** values from the script output. You will need these values in the next step.

```powershell
SUCCESS
Client ID: ae2386e6-799e-4f75-b191-855d7e691c75
Tenant ID: 5927c10a-91bd-4408-9c70-c50bce922b71
Client secret: ...
Secret expires: 10/28/2024 5:01:45 PM
```

## Configure the sample

1. Rename [oAuth.properties.example](./graphapponlytutorial/app/src/main/resources/graphapponlytutorial/oAuth.properties.example) to `oauthProperties` and update the values according to the following table.

| Setting | Value |
|---------|-------|
| `app.clientId` | The client ID of your app registration |
| `app.clientSecret` | The client secret |
| `app.tenantId` | The tenant ID of your organization |

## Build and run the sample

In your command-line interface (CLI), navigate to the project directory and run the following command.

```Shell
./gradlew --console plain run
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# <ScriptBody>
param(
[Parameter(Mandatory=$true,
HelpMessage="The app ID of the app registration")]
HelpMessage="The friendly name of the app registration")]
[String]
$AppId,
$AppName,

[Parameter(Mandatory=$true,
HelpMessage="The application permission scopes to configure on the app registration")]
Expand All @@ -21,15 +21,27 @@ param(
$graphAppId = "00000003-0000-0000-c000-000000000000"

# Requires an admin
Connect-MgGraph -Scopes "Application.ReadWrite.All AppRoleAssignment.ReadWrite.All User.Read" `
-UseDeviceAuthentication -ErrorAction Stop
Connect-MgGraph -Scopes "Application.ReadWrite.All User.Read" -UseDeviceAuthentication -ErrorAction Stop

# Get context for access to tenant ID
$context = Get-MgContext -ErrorAction Stop
$authTenant = $context.TenantId

# Get the application and service principal
$appRegistration = Get-MgApplication -Filter ("appId eq '" + $AppId +"'") -ErrorAction Stop
$appServicePrincipal = Get-MgServicePrincipal -Filter ("appId eq '" + $AppId + "'") -ErrorAction Stop
# Create app registration
$appRegistration = New-MgApplication -DisplayName $AppName -SignInAudience "AzureADMyOrg" -ErrorAction Stop
Write-Host -ForegroundColor Cyan "App registration created with app ID" $appRegistration.AppId

# Create corresponding service principal
$appServicePrincipal = New-MgServicePrincipal -AppId $appRegistration.AppId -ErrorAction SilentlyContinue `
-ErrorVariable SPError
if ($SPError)
{
Write-Host -ForegroundColor Red "A service principal for the app could not be created."
Write-Host -ForegroundColor Red $SPError
Exit
}

Write-Host -ForegroundColor Cyan "Service principal created"

# Lookup available Graph application permissions
$graphServicePrincipal = Get-MgServicePrincipal -Filter ("appId eq '" + $graphAppId + "'") -ErrorAction Stop
Expand Down Expand Up @@ -59,6 +71,7 @@ Write-Host -ForegroundColor Cyan "Added application permissions to app registrat
# Add admin consent
foreach ($appRole in $resourceAccess)
{
$appServicePrincipal
New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $appServicePrincipal.Id `
-PrincipalId $appServicePrincipal.Id -ResourceId $graphServicePrincipal.Id `
-AppRoleId $appRole.Id -ErrorAction SilentlyContinue -ErrorVariable SPError | Out-Null
Expand All @@ -77,8 +90,10 @@ $clientSecret = Add-MgApplicationPassword -ApplicationId $appRegistration.Id -Pa

Write-Host
Write-Host -ForegroundColor Green "SUCCESS"
Write-Host -ForegroundColor Cyan -NoNewline "Client ID: "
Write-Host -ForegroundColor Yellow $appRegistration.AppId
Write-Host -ForegroundColor Cyan -NoNewline "Tenant ID: "
Write-Host -ForegroundColor Yellow $context.TenantId
Write-Host -ForegroundColor Yellow $authTenant
Write-Host -ForegroundColor Cyan -NoNewline "Client secret: "
Write-Host -ForegroundColor Yellow $clientSecret.SecretText
Write-Host -ForegroundColor Cyan -NoNewline "Secret expires: "
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions app-auth/graphapponlytutorial/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.4.2/userguide/building_java_projects.html
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

// <DependenciesSnippet>
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'

// This dependency is used by the application.
implementation 'com.google.guava:guava:31.1-jre'
implementation 'com.azure:azure-identity:1.6.1'
implementation 'com.microsoft.graph:microsoft-graph:5.40.0'
}
// </DependenciesSnippet>

application {
// Define the main class for the application.
mainClass = 'graphapponlytutorial.App'
}

// <StandardInputSnippet>
run {
standardInput = System.in
}
// </StandardInputSnippet>
Loading