進行第一個 API 呼叫
這個頁面說明如何對應用程式和遊戲進行第一次 API 呼叫。
API 呼叫模式
下圖顯示用來建立新報表範本、排程自定義報表及擷取失敗數據的 API 呼叫模式。
此清單提供有關 API 呼叫模式圖表的詳細數據。
- 用戶端應用程式可以藉由呼叫 建立報表查詢 API 來定義自定義報表架構/範本。 或者,您可以從系統查詢清單使用報表範本
(QueryId)
。 - 成功時,建立 報表範本 API 會傳
QueryId
回 。 - 用戶端應用程式接著會使用 來呼叫 建立報表 API ,以及報表開始日期、重複間隔、週期和選擇性回呼
QueryID
URI。 - 成功時,建立 報表 API 會傳
ReportID
回 。 - 用戶端應用程式會呼叫 狀態 API 以取得報表的狀態。
- 用戶端應用程式接著會使用 取得報表執行 API 來查詢具有 和 日期範圍的報表
Report ID
狀態。 - 成功時,會傳回報表下載連結,而且應用程式可以起始數據的下載。
指定報表查詢語言
雖然我們提供 可用來建立報表的系統查詢 ,但您也可以根據業務需求建立自己的查詢。 若要深入瞭解自定義查詢,請參閱 自定義查詢規格。
驗證
首先,完成 使用 Microsoft 市集分析 API 的必要條件,以上線至合作夥伴中心帳戶。 接下來, 取得Microsoft Entra 存取令牌。 請只遵循步驟 1 和步驟 2。 步驟 3 是此流程的備援。
以程序設計方式呼叫 API
依照上一節所述取得Microsoft Entra Token 之後,請遵循下列步驟來建立您的第一個程式設計存取報告。
您可以從下列資料集下載資料(datasetName):
報表名稱 | API 中的數據集名稱 |
---|---|
Acquisition | 收購數 |
附加元件下載 | AddOnAcquisitions |
通道和轉換 | ChannelsAndConversions |
Gamepass 使用方式 | GamePass |
Gamepass 效能 | GamePassPurchase |
健康情況:當機和事件 | HealthFailureHits |
安裝 | 安裝 |
Ratings | Ratings |
評論 | 評論 |
永續性 | 永續性 |
使用量-每日 | UsageDaily |
Usage-Monthly | UsageMonthly |
Wishlist | Wishlist |
事件參與 | Xevents_Metrics |
定價促銷 - 彈性 | Xprice_Flexible_Offer |
定價促銷 - 目標 | Xprice_Targeted_Offer |
下列各節示範如何以程序設計方式從下載數據集存取內容的範例。
使用取得數據集 API 進行 REST 呼叫
API 回應會提供您可以從中下載報表的數據集名稱。 針對特定數據集,API 回應也會提供可用於自訂報表範本的可選取數據行清單。
建立報表查詢 API
此 API 有助於建立自訂查詢,以定義需要匯出數據行和計量的數據集。 API 提供根據業務需求建立新報告範本的彈性。
您也可以使用 我們提供的系統查詢 。 當不需要自定義報表範本時,您可以使用我們提供之系統查詢的 QueryId 直接呼叫建立報表 API。
要求範例
curl
--location
--request GET https://manage.devcenter.microsoft.com/consumer/insights/v1.1 /ScheduledDataset' \
--header 'Authorization: Bearer <AzureADToken>'
回應範例
{
"value": [
{
"columnFilters": {},
"aggregationToDateRangeMapping": {
"Hourly": "LAST_72_HOURS",
"Daily": "LAST_30_DAYS,LAST_3_MONTHS",
"Weekly": "LAST_6_MONTHS,LAST_12_MONTHS",
"Monthly": "LAST_2_YEARS,LAST_3_YEARS,LAST_4_YEARS"
},
"datasetName": "Acquisitions",
"selectableColumns": [
"TitleId",
"ProductId",
"XboxProductId",
"ProductTypeName",
"TitleName",
"CatalogId",
"SandboxId",
"SkuId",
"SkuTypeName",
"SkuDisplayName",
"AvailabilityId",
"RegionName",
"CountryName",
"Market",
"PaymentType",
"StoreClientName",
"StoreClientCategory",
"ParentProductName",
"ParentProductId",
"XboxParentProductId",
"AcquisitionType",
"PurchaseTaxType",
"LocalCurrencyCode",
"SupportedPlatform",
"Age",
"Gender",
"OsVersion",
"DeviceType",
"DateStamp"
],
"availableMetrics": [
"PurchaseQuantity",
"PurchasePriceUSDAmount",
"PurchaseTaxUSDAmount",
"PurchasePriceLocalAmount",
"PurchaseTaxLocalAmount"
],
"availableDateRanges": [
"LAST_72_HOURS",
"LAST_30_DAYS",
"LAST_3_MONTHS",
"LAST_6_MONTHS",
"LAST_12_MONTHS",
"LAST_2_YEARS",
"LAST_3_YEARS",
"LAST_4_YEARS"
],
"minimumRecurrenceInterval": 1
}
}
建立自訂查詢
在此步驟中,我們會為我們想要的報表建立自定義查詢。 每次需要報表時,都會使用這個建立的查詢(execute now
或 schedule
)。
要求範例
curl
--location
--request POST ' https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledQueries' \
--header ' Authorization: Bearer <AzureAD_Token>' \
--header 'Content-Type: application/json' \
--data-raw
'{
"Query": "SELECT TitleId, ProductId, XboxProductId, ProductTypeName, TitleName, CatalogId, SandboxId, SkuId, SkuTypeName, SkuDisplayName, AvailabilityId, RegionName, CountryName, Market, PaymentType, StoreClientName, StoreClientCategory, ParentProductName, ParentProductId, XboxParentProductId, AcquisitionType, PurchaseTaxType, LocalCurrencyCode, SupportedPlatform, Age, Gender, OsVersion, DeviceType, PurchasePriceUSDAmount, PurchaseTaxUSDAmount, PurchasePriceLocalAmount, PurchaseTaxLocalAmount FROM Acquisitions WHERE ProductId IN ('all') TIMESPAN LAST_30_DAYS AGGREGATED Daily",
"Name": "Consumer public API Create query",
"Description": "Acquisition query creation."
}'
回應範例
{
"value": [
{
"ProductInfo": {
"productGroupId": "",
"productId": "all",
"productIdDbColumnName": "ProductId"
},
"queryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"name": "Consumer public API Create query",
"description": "Acquisition query creation",
"query": "SELECT TitleId, ProductId, XboxProductId, ProductTypeName, TitleName, CatalogId, SandboxId, SkuId, SkuTypeName, SkuDisplayName, AvailabilityId, RegionName, CountryName, Market, PaymentType, StoreClientName, StoreClientCategory, ParentProductName, ParentProductId, XboxParentProductId, AcquisitionType, PurchaseTaxType, LocalCurrencyCode, SupportedPlatform, Age, Gender, OsVersion, DeviceType, PurchasePriceUSDAmount, PurchaseTaxUSDAmount, PurchasePriceLocalAmount, PurchaseTaxLocalAmount FROM Acquisitions TIMESPAN LAST_30_DAYS AGGREGATED Daily",
"type": "userDefined",
"user": "",
"createdTime": "2024-03-26T11:26:48Z"
}
],
"totalCount": 1,
"message": "Query created successfully",
"statusCode": 200
}
成功執行查詢時, 會產生需要用來產生報表的 queryId 。
取得查詢
列出所有可用的查詢。 在上述步驟中建立的現有查詢應該會在這裡反映。
curl --location 'https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledQueries' \
--header 'Authorization: Bearer <token> \
回應範例
{
"value": [
{
"ProductInfo": {
"productGroupId": "",
"productId": "all",
"productIdDbColumnName": "ProductId"
},
"queryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"name": "Consumer public API Create query",
"description": "Acquisition query creation",
"query": "SELECT TitleId, ProductId, XboxProductId, ProductTypeName, TitleName, CatalogId, SandboxId, SkuId, SkuTypeName, SkuDisplayName, AvailabilityId, RegionName, CountryName, Market, PaymentType, StoreClientName, StoreClientCategory, ParentProductName, ParentProductId, XboxParentProductId, AcquisitionType, PurchaseTaxType, LocalCurrencyCode, SupportedPlatform, Age, Gender, OsVersion, DeviceType, PurchasePriceUSDAmount, PurchaseTaxUSDAmount, PurchasePriceLocalAmount, PurchaseTaxLocalAmount FROM Acquisitions TIMESPAN LAST_30_DAYS AGGREGATED Daily",
"type": "userDefined",
"user": "",
"createdTime": "2024-03-26T11:26:48Z"
},
{
"ProductInfo": {
"productGroupId": "",
"productId": "9PDC2J734M08",
"productIdDbColumnName": "ProductId"
},
"queryId": "724c796e-ea64-438f-b784-f2e284349d2f",
"name": "Acquisition_Daily_30days_next2months",
"description": null,
"query": "SELECT TitleId, ProductId, XboxProductId, ProductTypeName, TitleName, CatalogId, SandboxId, SkuId, SkuTypeName, SkuDisplayName, AvailabilityId, RegionName, CountryName, Market, PaymentType, StoreClientName, StoreClientCategory, ParentProductName, ParentProductId, XboxParentProductId, AcquisitionType, PurchaseTaxType, LocalCurrencyCode, SupportedPlatform, Age, Gender, OsVersion, DeviceType, DateStamp, PurchaseQuantity, PurchasePriceUSDAmount, PurchaseTaxUSDAmount, PurchasePriceLocalAmount, PurchaseTaxLocalAmount FROM Acquisitions TIMESPAN LAST_30_DAYS AGGREGATED Daily",
"type": "userDefined",
"user": "",
"createdTime": "2024-01-23T17:21:42Z"
}
],
"totalCount": 2,
"message": "Queries fetched successfully",
"statusCode": 200
}
建立立即異步報表
在此步驟中,我們使用先前產生的 QueryId 來建立報表。 下列查詢可用來立即執行報表。 報表產生是異步的,需要個別的 API 呼叫才能擷取報表。
要求範例
curl --location 'https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledReport' \
--header 'Authorization: Bearer {{token}} \
--header 'Content-Type: application/json' \
--data '{
"Description": "Acquisition report",
"QueryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"ReportName": "Create Report - Acquisition",
"executeNow": true
}'
回應範例
{
"value": [
{
"productInfo": {
"productGroupId": "",
"productId": "all",
"productIdDbColumnName": "ProductId"
},
"reportId": "b58f9802-b118-485f-a0f1-edc273dea275",
"reportName": "Create Report - Acquisition",
"description": " Acquisition report ",
"queryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"query": "SELECT TitleId, ProductId, XboxProductId, ProductTypeName, TitleName, CatalogId, SandboxId, SkuId, SkuTypeName, SkuDisplayName, AvailabilityId, RegionName, CountryName, Market, PaymentType, StoreClientName, StoreClientCategory, ParentProductName, ParentProductId, XboxParentProductId, AcquisitionType, PurchaseTaxType, LocalCurrencyCode, SupportedPlatform, Age, Gender, OsVersion, DeviceType, PurchasePriceUSDAmount, PurchaseTaxUSDAmount, PurchasePriceLocalAmount, PurchaseTaxLocalAmount FROM Acquisitions TIMESPAN LAST_30_DAYS AGGREGATED Daily",
"user": "",
"createdTime": "",
"modifiedTime": null,
"executeNow": true,
"queryStartTime": null,
"queryEndTime": null,
"startTime": "2024-03-26T11:33:16Z",
"reportStatus": "Active",
"recurrenceInterval": -1,
"recurrenceCount": 1,
"callbackUrl": null,
"callbackMethod": null,
"format": "csv",
"endTime": "2024-03-26T11:33:16Z",
"totalRecurrenceCount": 1,
"nextExecutionStartTime": null
}
],
"totalCount": 1,
"message": "Report created successfully",
"statusCode": 200
}
reportId: 'execution' 會產生。 此標識碼必須用來排程報表的下載。
注意
如需欄位的詳細資訊 totalRecurrenceCount
,請參閱 瞭解排程報表的 totalRecurrenceCount 字段。
下載立即報告
要求範例
curl --location 'https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledReport/execution/b58f9802-b118-485f-a0f1-edc273dea275' \
--header 'Authorization: Bearer <token>' \
回應範例
{
"value": [
{
"executionId": "28016f06-6bbf-459e-ba30-429da6910192",
"reportId": "b58f9802-b118-485f-a0f1-edc273dea275",
"recurrenceInterval": -1,
"recurrenceCount": 1,
"callbackUrl": null,
"callbackMethod": null,
"format": "csv",
"executionStatus": "Completed",
"reportLocation": "https://pxanltx.blob.core.windows.net/programmatic-export-pi/Create Report - Acquisition.csv",
"reportAccessSecureLink": "https://pxanltx.blob.core.windows.net/programmatic-export-pi/Create Report - Acquisition.csv? <SAS token> ",
"reportExpiryTime": null,
"reportGeneratedTime": "2024-03-26T11:46:19Z",
"endTime": "2024-03-26T11:33:16Z",
"totalRecurrenceCount": 1,
"nextExecutionStartTime": null
}
],
"totalCount": 1,
"message": null,
"statusCode": 200
}
您可以叫用 reportAccessSecureLink 來下載報表。
建立排程報告
API 會呼叫協助建立排程報告。
要求
curl --location 'https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledReport' \
--header 'Authorization: Bearer <token> \
--header 'Content-Type: application/json' \
--data '{
"Description": "Creating a scheduled report",
"QueryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"ReportName": "Create scheduled report - Acquisition",
"StartTime": "2024-03-26T18:00:19Z",
"RecurrenceCount": 49,
"RecurrenceInterval": 1
}'
回應
{
"value": [
{
"productInfo": {
"productGroupId": "",
"productId": "all",
"productIdDbColumnName": "ProductId"
},
"reportId": "5e49796b-8146-4d98-9dde-aa14d2f78f0f",
"reportName": "Create scheduled report - Acquisition",
"description": "Acquisition description",
"queryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"query": "SELECT TitleId, ProductId, XboxProductId, ProductTypeName, TitleName, CatalogId, SandboxId, SkuId, SkuTypeName, SkuDisplayName, AvailabilityId, RegionName, CountryName, Market, PaymentType, StoreClientName, StoreClientCategory, ParentProductName, ParentProductId, XboxParentProductId, AcquisitionType, PurchaseTaxType, LocalCurrencyCode, SupportedPlatform, Age, Gender, OsVersion, DeviceType, PurchasePriceUSDAmount, PurchaseTaxUSDAmount, PurchasePriceLocalAmount, PurchaseTaxLocalAmount FROM Acquisitions TIMESPAN LAST_30_DAYS AGGREGATED Daily",
"user": "",
"createdTime": "2024-03-26T11:38:20Z",
"modifiedTime": null,
"executeNow": false,
"queryStartTime": null,
"queryEndTime": null,
"startTime": "2024-03-26T18:00:19Z",
"reportStatus": "Active",
"recurrenceInterval": 1,
"recurrenceCount": 49,
"callbackUrl": null,
"callbackMethod": null,
"format": "csv",
"endTime": "2024-03-28T18:00:19Z",
"totalRecurrenceCount": 49,
"nextExecutionStartTime": "2024-03-26T17:00:19Z"
}
],
"totalCount": 1,
"message": "Report created successfully",
"statusCode": 200
}
取得報告狀態和下載詳細數據
既然我們已建立報表,我們將進行 API 呼叫以取得報表狀態及其可下載的連結,以便將報表下載到用戶端。 為了進行此呼叫,我們會使用在上一個步驟中產生的相同 reportId 進行查詢。
要求
curl --location 'https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledReport/execution/3b6c1ec2-53c2-48f6-9c9b-a46e5ca69d7d' \
--header 'Authorization: Bearer<token>' \
回應
{
"value": [
{
"executionId": "28016f06-6bbf-459e-ba30-429da6910192",
"reportId": "5e49796b-8146-4d98-9dde-aa14d2f78f0f",
"recurrenceInterval": -1,
"recurrenceCount": 1,
"callbackUrl": null,
"callbackMethod": null,
"format": "csv",
"executionStatus": "Completed",
"reportLocation": "https://pxanltx.blob.core.windows.net/programmatic-export-pi/Create Report - Acquisition.csv",
"reportAccessSecureLink": "https://pxanltx.blob.core.windows.net/programmatic-export-pi/Create Report - Acquisition.csv? <SAS token> ",
"reportExpiryTime": null,
"reportGeneratedTime": "2024-03-26T11:46:19Z",
"endTime": "2024-03-26T11:33:16Z",
"totalRecurrenceCount": 1,
"nextExecutionStartTime": null
}
],
"totalCount": 1,
"message": null,
"statusCode": 200
}
使用 Webhook 建立排程報表
Webhook 會在報表就緒時立即作為叫用的端點。 必須提供 callbackURL 參數。
合作夥伴必須撰寫其 Webhook 處理程式。 在上一個範例中,一旦報表準備就緒,就會叫用 『https://msnotify.com' 做為通知。 在叫用中,合作夥伴可以取得排程報告及其狀態的清單,然後使用上述 API 來下載檔案。
Request
curl --location 'https://manage.devcenter.microsoft.com/consumer/insights/v1.1/ScheduledReport' \
--header 'Authorization: Bearer<token>' \
--header 'Content-Type: application/json' \
--header 'Cookie: ApplicationGatewayAffinity=3ebb3a6588e1f91ad543ccd7cdf31ec0; ApplicationGatewayAffinityCORS=3ebb3a6588e1f91ad543ccd7cdf31ec0' \
--data '{
"Description": "Acquisition report",
"QueryId": "f17f8c8b-5980-40e0-a6f9-7df0c867b615",
"ReportName": "Create scheduled report - Acquisition",
"StartTime": "2024-03-26T18:00:19Z",
"RecurrenceCount": 49,
"RecurrenceInterval": 1,
"callbackURL": "https://msnotify.com",
"callbackMethod": "get"
}'
API 文件
請參閱 OpenAPI 規格。 在公用 Swagger 編輯器中貼上規格的內容,以檢視 API,並以慣用的語言產生用戶端(C#、python 等),以取用 API。
重要
此 API 已針對 executionStatus=Completed
和 getLatestExecution=true
設定預設查詢參數。 因此,在報表第一次成功執行之前呼叫 API 會傳回 404 錯誤。 您可以藉由設定 executionStatus=Pending
來取得暫止執行。