ストア サービスを使った分析データへのアクセス

Microsoft Store 分析 API を使用すると、自分または自分の組織の Windows パートナー センター アカウントに登録されているアプリの分析データをプログラムで取得できます。 この API では、アプリおよびアドオン (アプリ内製品または IAP とも呼ばれます) の入手数、エラー、アプリの評価とレビューに関するデータを取得できます。 この API は、Azure Active Directory (Azure AD) を使って、アプリまたはサービスからの呼び出しを認証します。

次の手順で、このプロセスについて詳しく説明しています。

  1. すべての前提条件を完了したことを確認します。
  2. Microsoft Store 分析 API のメソッドを呼び出す前に、Azure AD アクセス トークンを取得する必要があります。 トークンを取得した後、Microsoft Store 分析 API の呼び出しでこのトークンを使用できるのは、その有効期限が切れるまでの 60 分間です。 トークンの有効期限が切れた後は、新しいトークンを生成できます。
  3. Microsoft Store 分析 API を呼び出します

手順 1: Microsoft Store 分析 API を使うための前提条件を満たす

Microsoft Store 分析 API を呼び出すコードの作成を開始する前に、次の前提条件が満たされていることを確認します。

  • 自分 (または自分の組織) に Azure AD ディレクトリがあり、自分がそのディレクトリに対するグローバル管理者のアクセス許可を持っている必要があります。 Microsoft 365 または Microsoft の他のビジネス サービスをすでに使用している場合、Azure AD ディレクトリをすでに所有しています。 それ以外の場合は、追加料金なしにパートナー センターで新しい Azure AD を作成できます。

  • Azure AD アプリケーションをパートナー センター アカウントに関連付け、アプリケーションのテナント ID とクライアント ID を取得してキーを生成する必要があります。 Azure AD アプリケーションは、Microsoft Store 分析 API の呼び出し元のアプリまたはサービスを表します。 API に渡す Azure AD アクセス トークンを取得するには、テナント ID、クライアント ID、キーが必要です。

    Note

    この作業を行うのは一度だけです。 テナント ID、クライアント ID、キーがあれば、新しい Azure AD アクセス トークンを作成する必要がある度にそれらを再利用できます。

Azure AD アプリケーションをパートナー センター アカウントに関連付け、必要な値を取得するには、次の手順を実行します。

  1. パートナー センターで、組織のパートナー センター アカウントを組織の Azure AD ディレクトリに関連付けます

  2. 次に、パートナー センターの [アカウント設定] セクションの [ユーザー] ページで、パートナー センター アカウントの分析データへのアクセスに使用するアプリやサービスを表す Azure AD アプリケーションを追加します。 このアプリケーションにマネージャー ロールを確実に割り当てます。 アプリケーションがまだ Azure AD ディレクトリに存在しない場合、パートナー センターで新しい Azure AD アプリケーションを作成できます。

  3. [ユーザー] ページに戻り、Azure AD アプリケーションの名前をクリックしてアプリケーション設定に移動し、テナント IDクライアント ID の値を書き留めます。

  4. [新しいキーを追加] をクリックします。 次の画面で、キーの値を書き留めます。 このページを離れると、この情報にアクセスすることはできなくなります。 詳細については、「Azure AD アプリケーションのキーを管理する」を参照してください。

手順 2:Azure AD アクセス トークンを取得する

Microsoft Store 分析 API のいずれかのメソッドを呼び出す前に、まず API の各メソッドの Authorization ヘッダーに渡す Azure AD アクセス トークンを取得する必要があります。 アクセス トークンを取得したら、期限が切れる 60 分が経過する前に使用します。 トークンの有効期限が切れた後は、トークンを更新してそれ以降の API 呼び出しで引き続き使用できます。

アクセス トークンを取得するには、「クライアント資格情報を使用したサービス間の呼び出し」の手順に従って、HTTP POST を https://login.microsoftonline.com/<tenant_id>/oauth2/token エンドポイントに送信します。 要求の例を次に示します。

POST https://login.microsoftonline.com/<tenant_id>/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded; charset=utf-8

grant_type=client_credentials
&client_id=<your_client_id>
&client_secret=<your_client_secret>
&resource=https://manage.devcenter.microsoft.com

POST URI の tenant_id の値と client_id および client_secret のパラメーターには、前のセクションでパートナー センターから取得したアプリケーションのテナント ID、クライアント ID、キーを指定します。 resource パラメーターには、https://manage.devcenter.microsoft.com を指定します。

アクセス トークンの有効期限が切れた後は、この手順に従って更新できます。

> [!NOTE]
> ResourceType='Graph.windows.net' will be deprecated after September 2023. Please migrate to ResourceType ='Graph.microsoft.com'

手順 3: Microsoft Store 分析 API を呼び出す

Azure AD アクセス トークンを取得したら、Microsoft Store 分析 API を呼び出すことができます。 各メソッドの Authorization ヘッダーにアクセス トークンを渡す必要があります。

UWP アプリおよびゲーム用のメソッド

アプリとゲームの入手数とアドオンの入手数には、以下のメソッドを使用できます。

UWP アプリ向けのメソッド

次の分析メソッドを、パートナー センターの UWP アプリで利用できます。

シナリオ メソッド
取得、変換、インストール、および使用状況
アプリのエラー
洞察
評価とレビュー
アプリ内広告と広告キャンペーン

デスクトップ アプリケーション向けのメソッド

次の分析メソッドは、Windows デスクトップ アプリケーション プログラムに参加している開発者アカウントで利用できます。

シナリオ メソッド
インストール
ブロック
アプリケーション エラー
洞察

Xbox Live サービス向けのメソッド

次の追加のメソッドは、Xbox Live サービスを使うゲームの開発者アカウントで利用できます。 Xbox 用 Microsoft Store Analytics API は使用できなくなりました。 gaming/xbox-live/get-started/join-dev-program/join-dev-program_nav

シナリオ メソッド
一般的な分析

ハードウェアとドライバー向けのメソッド

Windows ハードウェア ダッシュボード プログラムに属している開発者アカウントは、ハードウェアおよびドライバーの分析データを取得するために、メソッドの追加セットにアクセスできます。 詳細については、「ハードウェア ダッシュボード API」を参照してください。

コードの例

次のコード例は、Azure AD アクセス トークンを取得し、C# コンソール アプリから Microsoft Store 分析 API を呼び出す方法を示しています。 このコード例を使う場合は、変数 tenantIdclientIdclientSecret、および appID を自分のシナリオに合った適切な値に割り当ててください。 この例では、Microsoft Store 分析 API から返される JSON データを逆シリアル化するときに、Newtonsoft の Json.NET パッケージが必要になります。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace TestAnalyticsAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string tenantId = "<your tenant ID>";
            string clientId = "<your client ID>";
            string clientSecret = "<your secret>";

            string scope = "https://manage.devcenter.microsoft.com";

            // Retrieve an Azure AD access token
            string accessToken = GetClientCredentialAccessToken(
                    tenantId,
                    clientId,
                    clientSecret,
                    scope).Result;

            // This is your app's Store ID. This ID is available on
            // the App identity page of the Dev Center dashboard.
            string appID = "<your app's Store ID>";

            DateTime startDate = DateTime.Parse("08-01-2015");
            DateTime endDate = DateTime.Parse("11-01-2015");
            int pageSize = 1000;
            int startPageIndex = 0;

            // Call the Windows Store analytics API
            CallAnalyticsAPI(accessToken, appID, startDate, endDate, pageSize, startPageIndex);

            Console.Read();
        }

        private static void CallAnalyticsAPI(string accessToken, string appID, DateTime startDate, DateTime endDate, int top, int skip)
        {
            string requestURI;

            // Get app acquisitions
            requestURI = string.Format(
                "https://manage.devcenter.microsoft.com/v1.0/my/analytics/appacquisitions?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}",
                appID, startDate, endDate, top, skip);

            //// Get add-on acquisitions
            //requestURI = string.Format(
            //    "https://manage.devcenter.microsoft.com/v1.0/my/analytics/inappacquisitions?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}",
            //    appID, startDate, endDate, top, skip);

            //// Get app failures
            //requestURI = string.Format(
            //    "https://manage.devcenter.microsoft.com/v1.0/my/analytics/failurehits?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}",
            //    appID, startDate, endDate, top, skip);

            //// Get app ratings
            //requestURI = string.Format(
            //    "https://manage.devcenter.microsoft.com/v1.0/my/analytics/ratings?applicationId={0}&startDate={1}&endDate={2}top={3}&skip={4}",
            //    appID, startDate, endDate, top, skip);

            //// Get app reviews
            //requestURI = string.Format(
            //    "https://manage.devcenter.microsoft.com/v1.0/my/analytics/reviews?applicationId={0}&startDate={1}&endDate={2}&top={3}&skip={4}",
            //    appID, startDate, endDate, top, skip);

            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestURI);
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            WebRequestHandler handler = new WebRequestHandler();
            HttpClient httpClient = new HttpClient(handler);

            HttpResponseMessage response = httpClient.SendAsync(requestMessage).Result;

            Console.WriteLine(response);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);

            response.Dispose();
        }

        public static async Task<string> GetClientCredentialAccessToken(string tenantId, string clientId, string clientSecret, string scope)
        {
            string tokenEndpointFormat = "https://login.microsoftonline.com/{0}/oauth2/token";
            string tokenEndpoint = string.Format(tokenEndpointFormat, tenantId);

            dynamic result;
            using (HttpClient client = new HttpClient())
            {
                string tokenUrl = tokenEndpoint;
                using (
                    HttpRequestMessage request = new HttpRequestMessage(
                        HttpMethod.Post,
                        tokenUrl))
                {
                    string content =
                        string.Format(
                            "grant_type=client_credentials&client_id={0}&client_secret={1}&resource={2}",
                            clientId,
                            clientSecret,
                            scope);

                    request.Content = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded");

                    using (HttpResponseMessage response = await client.SendAsync(request))
                    {
                        string responseContent = await response.Content.ReadAsStringAsync();
                        result = JsonConvert.DeserializeObject(responseContent);
                    }
                }
            }

            return result.access_token;
        }
    }
}

エラー応答

Microsoft Store 分析 API は、エラー コードとメッセージが含まれた JSON オブジェクトにエラー応答を返します。 次の例は、無効なパラメーターに対するエラー応答を示しています。

{
    "code":"BadRequest",
    "data":[],
    "details":[],
    "innererror":{
        "code":"InvalidQueryParameters",
        "data":[
            "top parameter cannot be more than 10000"
        ],
        "details":[],
        "message":"One or More Query Parameters has invalid values.",
        "source":"AnalyticsAPI"
    },
    "message":"The calling client sent a bad request to the service.",
    "source":"AnalyticsAPI"
}