M365, Intune, EM+S, Azure, Power Apps, Power Automate
Use the Microsoft Graph API and the Android Device Provisioning Partner API to automatic replace profile tokens for Android Enterprise dedicated devices
<#
.SYNOPSIS
Script to get a refresh token to Android Device Provisioning Partner API for unattended access.
.DESCRIPTION
This script uses the client_id and the client_secret from the client_secret*.json with interactive login to generate a authorization code. The authorization code is used with the client_id and the client_secret to get a refresh token. To use this script you first need to set up API access in the Google cloud console portal and create credentials and download the client_secret*.json file.
Run this script manually. Start part 1 by adding the path to the downloaded client_secret*.json file. Start part 2 by adding the authorization code from the redirected url in the browser.
Follow the instructions on the Google developer homepage to turn on the zero-touch enrollment API. https://developers.google.com/zero-touch/guides/customer/quickstart/dotnet
.NOTES
Written by: Tobias Renström
Blog: https://cloudnstuff.com/
Version: 1.0
#>
## Part 1
# Path to the downloaded client_secret*.json file
$authJsonPath = ""
$zeroTouchAuthJson = Get-Content -Path $authJsonPath -Raw | ConvertFrom-Json
$zeroTouchClientId = $zeroTouchAuthJson.web.client_id
$zeroTouchClientSecret = $zeroTouchAuthJson.web.client_secret
$redirectUrl = "http://localhost/oauth2callback"
$scope = "https://www.googleapis.com/auth/androidworkzerotouchemm"
$authUrl = "https://accounts.google.com/o/oauth2/auth?redirect_uri=$redirectUrl&client_id=$zeroTouchClientId&scope=$scope&include_granted_scopes=true&prompt=consent&access_type=offline&response_type=code"
start msedge.exe -ArgumentList "$authUrl -inprivate"
## Part 2
# Copy the 'code' (authorization code) from the redirected url in the browser and put it here
$authorizationCode = ""
$requestUri = "https://oauth2.googleapis.com/token"
$requestBody = @{
code = $authorizationCode
client_id = $zeroTouchClientId
client_secret = $zeroTouchClientSecret
redirect_uri = $redirectUrl
grant_type = "authorization_code"
}
$tokens = Invoke-RestMethod -Method Post -Uri $requestUri -Body $requestBody
$refreshToken = $tokens.refresh_token
$refreshToken | Set-Clipboard
Logic Apps
Note: See my last blog post that i’m mention in the beginning of this blog post about how to set up the Microsoft Graph API part for the Logic Apps
Bearer Parse JSON – Parse Zero Touch Google token response body (body)?[‘access_token’]
The request body
Update Intune token for Zero Touch configuration JSON body (Compose)
Summary
This is an example how you can automate this and if you not want to use Logic Apps you can build this with PowerShell and use Azure Automation to run it. I have also workable solution built with PowerShell but I prefer to use Logic Apps for automating this. To understand all the parts, I recommend that you read my first blog post that I linked at the beginning of this blog post.
3 thoughts on “Use the Microsoft Graph API and the Android Device Provisioning Partner API to automatic replace profile tokens for Android Enterprise dedicated devices”
Hi, since you mention that you have a workable solution with powershell.
Im Trying to Format the Body to patch with the new Token and nothing seem to work.
Any Advice on that.
Hi, since you mention that you have a workable solution with powershell.
Im Trying to Format the Body to patch with the new Token and nothing seem to work.
Any Advice on that.
tried with HadTable
$hash = [ordered]@{
‘android.app.extra.PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED’ = $true
‘android.app.extra.PROVISIONING_ADMIN_EXTRAS_BUNDLE’ = @{ ‘com.google.android.apps.work.clouddpc.EXTRA_ENROLLMENT_TOKEN’ = $Token }
}
$Data = ConvertTo-Json -InputObject $hash
I get either 400 Error or it passes and clear the dpcExtras so it gets emptied out.
Hi Max,
How does your URI look like in PowerShell when you do the patch?
Did you get an error message in the response along with the 400 status code?
/Tobias
Hi, i figured it out in the end. My issue was the formatting of the body
$response = “”
$response = Invoke-RestMethod -Headers $header -Uri $requestUri -Method GET -ContentType ‘application/json’
$SettingsObject = $response.dpcExtras | ConvertFrom-Json
$oldTocken = $SettingsObject.’android.app.extra.PROVISIONING_ADMIN_EXTRAS_BUNDLE’.’com.google.android.apps.work.clouddpc.EXTRA_ENROLLMENT_TOKEN’
$Token = “”
$Config = $response.dpcExtras -replace ($oldTocken, $Token)
$hash = [ordered]@{
dpcExtras = $Config
}
$body = ConvertTo-Json -InputObject $hash