I'm trying to follow the steps in this link: https://github.com/MicrosoftDocs/partner-rest/blob/docs/partner-rest/develop/api-authentication.md
But after the creation of the app and user access, it doesn't show how to call the API. I'm trying to connect using Python/Spark, but can't go further because there's no documentation that explains how to do it step-by-step.
The code below is an example from the app-only authentication, it works but it doesn't allow to get some data that requires the app+user auth.
import requests, json
from pyspark.sql import SparkSession
from pyspark.sql.types import StructField, StringType, IntegerType, StructType
access_var = {
"auth_url": "https://login.microsoftonline.com/tenantid/oauth2/token",
"resource": "https://graph.windows.net",
"client_id": {mssparkutils.credentials.getSecret('https://vaultname.vault.azure.net/', 'KV-Client-ID')},
"client_st": {mssparkutils.credentials.getSecret('https://vaultname.vault.azure.net/', 'KV-Client-Secret')},
"grant_type": "client_credentials"
}
_schema = StructType([
StructField("Tenant ID", StringType(), True),
StructField("Domain", StringType(), True),
StructField("Company", StringType(), True),
StructField("URI", StringType(), True)
])
def get_access():
_body = {
"resource": access_var["resource"],
"client_id": access_var["client_id"],
"client_secret": access_var["client_st"],
"grant_type": access_var["grant_type"]
}
_headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(access_var["auth_url"], data=_body, headers=_headers)
response.raise_for_status()
return response.json()
response = get_access()
access_t = response["access_token"]
def catch(api_url):
"https://api.partnercenter.microsoft.com/v1/customers"
def fetch():
api_url = "https://api.partnercenter.microsoft.com/v1/customers"
api_auth = {
"Authorization": f"Bearer {access_t}",
"Accept": "application/json"
}
api_response = requests.get(api_url, headers=api_auth)
return api_response.json()
def create_df(spark, result_list):
return spark.createDataFrame(result_list, schema=_schema)
if __name__ == "__main__":
spark = SparkSession.builder.appName("API_CALL").getOrCreate()
all_results = []
data = fetch()
result_list = data
for item in result_list["items"]:
tenant_id = item["companyProfile"].get("tenantId", "N/A").strip()
domain = item["companyProfile"].get("domain", "N/A").strip()
company_name = item["companyProfile"].get("companyName").strip()
uri = item["links"]["self"]["uri"]
all_results.append((tenant_id, domain, company_name, uri))
df = create_df(spark, all_results)
df.show()
Register Azure AD application and grant
API permissionsof Delegated type as below:While using interactive flow, you must include redirect URI for the Mobile and Desktop applications platform:
Make sure to enable public client flows in your app registration like this:
Now, make use of below Python code to generate access token for Partner Center API using interactive flow that asks user to sign in:
Response:
You can decode the above token in jwt.ms and check whether
aud&scpclaims have valid values or not:Using the above token, you can make requests to Partner Center API with below updated python code:
Reference: Partner Center authentication - Partner app developer | Microsoft