Microsoft Partner API <> access denied

Viewed 531
3 Answers

The document shows us the permission of the api is "Delegated" type, so we can not use client credential flow to get the access token. We should get the token by username/password flow (shown like below screenshot).

enter image description here

You can copy your access token to this page, decode the token and check if it contains "scp": "user_impersonation"

Adding the progress here, too long for a comment.

Thank you for your answer! I'm progressing but still have a 401 :

  1. I get an access token from https://login.microsoftonline.com/{{TenantID}}/oauth2/token

response : { "token_type": "Bearer", "scope": "user_impersonation", "expires_in": "3599", "ext_expires_in": "3599", "expires_on": "1610540194", "not_before": "1610536294", "resource": "https://api.partnercenter.microsoft.com", "access_token": "....." }

  1. I get an access token from https://api.partnercenter.microsoft.com/generatetoken

response :

{ "access_token": "", "token_type": "bearer", "expires_in": 3585 }

But i still have a 401 from https://api.partner.microsoft.com/v1.0/engagements/referrals

Using python I did this. But not able to generate tokens that can help to get customer data. Code to get access token

url = "https://login.microsoftonline.com/"+tanat_id+"/oauth2/token"
headers = {
    "Accept": "application/json",
    "return-client-request-id": "true",
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
    "Host": "login.microsoftonline.com",
    "Content-Length": "194",
    "Expect": "100-continue"
}
data = {
    'client_id': client_id,
    'resource': 'https://api.partner.microsoft.com',
    'client_secret': client_secret,
    'grant_type': 'client_credentials',
}

res = requests.post(url, headers=headers, data=data)
access_token = json.loads(res.text)["access_token"]

Code to generate a token

url = "https://api.partnercenter.microsoft.com/generatetoken"
headers = {
    "Authorization": "Bearer " + str(access_token),
    "Accept": "application/json",
    "content-type": "application/json;charset=UTF-8"
}
data = {
    'grant_type': 'authorization_code',
}

res = requests.post(url, headers=headers, data={"grant_type":"jwt_token"})

But

>>> res.text
'{"error":"invalid_grant","error_description":"Invalid token: tokenValidationResult == null - True, tokenValidationResult.Principal == null - True, tokenValidationResult.Principal.Identity == null- True, tokenValidationResult.Principal.Identity.IsAuthenticated - "}'

Using access_token if I try to get customer list it returns a 401 error.

#Customer List

url = "https://api.partnercenter.microsoft.com/v1/customers?size=40"
headers = {
    "Authorization": "Bearer " + str(access_token),
    "Accept": "application/json",
}

res = requests.get(url, headers=headers)
res
<Response [401]>
res.text
""
Related