Search code examples
restapipowershell

Partner Central rest api 401 unauthorized access


I followed the Microsoft doc to get the billing profile of a customer. With the auth tutorial with the Powershell Code

$credential = Get-Credential
Connect-PartnerCenter -Credential $credential -ServicePrincipal -TenantId '<TenantId>'

Copied the access token and produced a Postman Get request but still got an 401 unauthorized request

enter image description here

It could be from the security update of Microsoft , but the Auth documentation is from january so i think These are the steps to get access to the parner central https://www.microsoftpartnercommunity.com/t5/UK-Partner-Zone-Discussions/FY19-CSP-program-new-mandatory-security-requirements/td-p/6981

Or I don't have the right permissions as a user to get the billing profile. I know it's one step that I oversee or that it's one thing that I did wrong but I can't see it

I am aware that there are some questions on stackoverflow about this issue. But Can't seem to find a solution there


Solution

  • Tried to get the access token with this Powershell code:

        $appId = 'xxxxxxxxxxxxxxxxxxxxx'
    $appSecret = 'xxxxxxxxxxxxxxxx' | ConvertTo-SecureString -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential $appId, $appSecret
    $token = New-PartnerAccessToken -Consent -Credential $credential -Resource https://api.partnercenter.microsoft.com -ServicePrincipal
    
    New-PartnerAccessToken -RefreshToken $token.RefreshToken -Resource https://api.partnercenter.microsoft.com -Credential $credential -ServicePrincipal
    

    Source and probably more explanation to this you can find here: docs If the link becomes invalid search for Partner Consent process for the Partner Center

    Hope this one helps you out, this was what worked for me. Struggled a lot to find this out also.

    Also make sure in your Azure AD app you select access token in the Authentication Setting beneath Implicit grant. And to select , urn:ietf:wg:oauth:2.0:oob beneath the suggested redirect URL's