Skip to content

Instantly share code, notes, and snippets.

@regisdiogo
Created December 29, 2022 10:58
Show Gist options
  • Save regisdiogo/13e2f217234ccd8d590c833dd2744d85 to your computer and use it in GitHub Desktop.
Save regisdiogo/13e2f217234ccd8d590c833dd2744d85 to your computer and use it in GitHub Desktop.
Testing Microsoft API Connection
$clientId = "[REDACTED]"
$clientSecret = "[REDACTED]"
$clientState = "my-app-state"
$redirect_uri = "https://localhost:8080"
$developerToken = "[REDACTED]";
Start-Process "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=$clientId&scope=openid%20profile%20https://ads.microsoft.com/msads.manage%20offline_access&response_type=code&redirect_uri=$redirect_uri&state=$clientState&prompt=login"
$code = Read-Host "Grant consent in the browser, and then enter the response URI here"
$code = $code -match 'code=(.*)\&'
$code = $Matches[1]
$response = Invoke-WebRequest https://login.microsoftonline.com/common/oauth2/v2.0/token -ContentType application/x-www-form-urlencoded -Method POST -Body "client_id=$clientId&scope=https://ads.microsoft.com/msads.manage%20offline_access&code=$code&grant_type=authorization_code&redirect_uri=$redirect_uri&client_secret=$clientSecret"
$oauthTokens = ($response.Content | ConvertFrom-Json)
Write-Output "Access token: " $oauthTokens.access_token
Write-Output "Access token expires in: " $oauthTokens.expires_in
Write-Output "Refresh token: " $oauthTokens.refresh_token
$accessToken = $oauthTokens.access_token;
[xml]$getUserRequest =
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v13="https://bingads.microsoft.com/Customer/v13">
<soapenv:Header>
<v13:DeveloperToken>{0}</v13:DeveloperToken>
<v13:AuthenticationToken>{1}</v13:AuthenticationToken>
</soapenv:Header>
<soapenv:Body>
<v13:GetUserRequest>
<v13:UserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</v13:GetUserRequest>
</soapenv:Body>
</soapenv:Envelope>' -f $developerToken, $accessToken
$headers = @{"SOAPAction" = "GetUser"}
$uri = "https://clientcenter.api.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc"
$response = Invoke-WebRequest $uri -Method post -ContentType 'text/xml' -Body $getUserRequest -Headers $headers
Write-Output $response.Content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment