1

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
""
prashant mavadiya
  • 176
  • 1
  • 2
  • 9

2 Answers2

1

You need to fetch the first access token from https://graph.windows.net

data = {
    'client_id': client_id,
    'resource': 'https://graph.windows.net',
    'client_secret': client_secret,
    'grant_type': 'client_credentials',
}

Kindly check the request headers here

navk
  • 11
  • 1
0

The first error "error": "invalid_grant" is because of the permissions, this usually occurs when it’s been long that the credentials got refreshed or Give a try with below steps:

• Select "Azure: Sign in"

• Removed the linked account.

• Add the account back.

401 is Unauthorized error: This error often means that the access token may be missing in the HTTP authenticate request header or that the token is invalid or has expired

For these common errors refer to the document

SaiKarri-MT
  • 1,174
  • 1
  • 3
  • 8