Combreal

mcapi.psm1

May 29th, 2021 (edited)
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Minecraft API with Powershell.
  4.  
  5. .DESCRIPTION
  6. This script allows you to interact with Minecraft accounts via its API with Powershell.
  7.  
  8. .NOTES
  9. Testing.
  10.  
  11. .EXAMPLE
  12. Import-Module .\mcapi.psm1
  13. New-MCQuery "GET" "/profile"
  14.  
  15. .LINK
  16. https://wiki.vg
  17. #>
  18.  
  19. $Global:MinecraftToken = $null
  20.  
  21. Function New-MCQuery([switch]$HideOutput, $RestMethod, $Query, $Arguments, $Data)
  22. {
  23.     <#
  24.     .SYNOPSIS
  25.     Send a rest query and display the result
  26.     #>
  27.  
  28.     $URL = "https://api.minecraftservices.com/minecraft" + $Query + $Arguments
  29.  
  30.     If($Data -eq $null)
  31.     {
  32.         $response = Invoke-RestMethod -Uri $URL -Method $RestMethod -Headers  @{ 'Authorization' = "Bearer $Global:MinecraftToken" }
  33.     }
  34.     Else
  35.     {
  36.         $response = Invoke-RestMethod -Uri $URL -Method $RestMethod -Headers  @{ 'Authorization' = "Bearer $Global:MinecraftToken" } -Body $json -ContentType 'application/json'
  37.     }
  38.  
  39.     $response | ConvertTo-Json
  40. }
  41.  
  42. Function Get-MCToken()
  43. {
  44.     <#
  45.     .SYNOPSIS
  46.     Get API token to build queries afterwards
  47.     #>
  48.  
  49.     $Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your Minecraft user name and password.", "", "Minecraft")
  50.  
  51.     $agent = @{
  52.         "name"="Minecraft"
  53.         "version"=1
  54.     }
  55.    
  56.     $getToken = @{
  57.         "username"=$Credential.UserName
  58.         "password"=$Credential.GetNetworkCredential().Password
  59.     }
  60.    
  61.     $getToken.Add("agent",$agent)
  62.    
  63.     $json = $getToken | ConvertTo-Json
  64.    
  65.     $response = Invoke-RestMethod 'https://authserver.mojang.com/authenticate' -Method 'POST' -Body $json -ContentType 'application/json'    
  66.     $Global:MinecraftToken = $response.accessToken
  67. }
  68.  
  69. Get-MCToken
  70.  
  71. Export-ModuleMember -Function *
Add Comment
Please, Sign In to add comment