BaaS認証によるショップビルダーを利用する
ショップビルダーをBaaSの認可システムと併用することで、ゲーム内アイテムを販売することができます。この場合の相互作用は次のとおりです:
- ユーザーは、BaaSの認証システムを介してアプリケーションにログインします。
- BaaSサービスは、ユーザーIDを渡してエクソーラサーバーにユーザーJSON Web Token(JWT)を要求します。
- エクソーラサーバーは、ユーザーJWTをBaaSサービスに返します。
- BaaSサービスは、ユーザーJWTをアプリケーションに渡します。
- アプリケーションはユーザーJWTを利用して、APIを使用してエクソーラサーバーと相互作用します。
お知らせ
ユーザー認証のロジックを実装していない場合は、エクソーラログインを統合し、PlayFabやFirebaseにユーザーデータの保存を設定することが可能です。これにより、ログインAPIを使用してユーザーを認証したり、JWTを受信して他のエクソーラ製品のAPIと相互作用することができます。
- アドミンページで、標準のログインプロジェクトをあなたのプロジェクトに接続します。
- サーバーOAuth 2.0クライアントをセットアップします。
- FirebaseとPlayFabの説明に従って、既成の機能をプロジェクトに追加してください。
サーバーOAuth 2.0クライアントをセットアップする
- アドミンページでプロジェクトを開き、ログインセクションに移動します。
- ログインプロジェクトのパネルでの構成するをクリックします。
- セキュリティブロックに移動してOAuth 2.0セクションに移動します。
- OAuth 2.0を追加するをクリックします。
- OAuth 2.0のリダイレクトURIを指定します。
- サーバー(サーバー・トゥ・サーバー接続)チェックボックスを入ります。
- 接続をクリックします。
- クライアントIDと秘密鍵をコピーして保存してください。
Firebaseプロジェクトにクラウド機能を追加する
- Firebaseプロジェクトを初期化します。
- ユーザーJWTの受信機能をインポートして設定します。そこで:
<ProjectID>
はプロジェクトIDで、アドミンページでプロジェクト名の横に表示されます。<LoginID>
は、アドミンページを開き、「ログイン > ダッシュボード > ログインプロジェクト」セクションに移動し、ログインプロジェクト名の横にある「IDをコピー」をクリックすることで取得できるログインIDです。<OAuthClientID>
はサーバーOAuth 2.0クライアントをセットアップする時に受信したクライアントIDです。<OAuthSecretKey>
はサーバーOAuth 2.0クライアントをセットアップする時に受信した秘密鍵です。
ユーザーJWTを受信するための機能コード:
Copy
- javascript
1const projectId = "<ProjectID>";
2const loginProjectId = "<LoginID>";
3
4const serverOauthClientId = <OAuthClientID>;
5const serverOauthClientSecret = "<OAuthSecretKey>";
6
7exports.getXsollaLoginToken = functions.https.onCall((data, context) => {
8 if (!context.auth) {
9 throw new functions.https.HttpsError(
10 "failed-precondition",
11 "The function must be called while authenticated."
12 );
13 }
14
15 const postData =
16 "grant_type=client_credentials" +
17 `&client_secret=${serverOauthClientSecret}`+
18 `&client_id=${serverOauthClientId}`;
19
20 const options = {
21 hostname: "login.xsolla.com",
22 port: 443,
23 path: "/api/oauth2/token",
24 method: "POST",
25 headers: {
26 "Content-Type": "application/x-www-form-urlencoded",
27 "Content-Length": postData.length,
28 },
29 };
30
31 return new Promise( (resolve, reject) => {
32 const req = https.request(options, (res) => {
33 if (res.statusCode !== 200) {
34 reject(
35 new functions.https.HttpsError(
36 "internal",
37 "Server token not received"
38 )
39 );
40 }
41 let body = [];
42 res.on("data", (d) => {
43 body.push(d);
44 });
45 res.on("end", () => {
46 try {
47 body = JSON.parse(Buffer.concat(body).toString());
48 } catch (e) {
49 reject(
50 new functions.https.HttpsError(
51 "internal",
52 "Malformed server token response"
53 )
54 );
55 }
56 getClientToken(context.auth.uid, body.access_token, resolve, reject);
57 });
58 });
59 req.on("error", (e) => {
60 reject(new functions.https.HttpsError(
61 "internal",
62 "Internal error while server token flow"
63 ));
64 });
65
66 req.write(postData);
67 req.end();
68 });
69});
70
71// eslint-disable-next-line require-jsdoc
72function getClientToken(userId, serverToken, resolve, reject) {
73 const postData = JSON.stringify(
74 {
75 "server_custom_id": userId,
76 }
77 );
78
79 const path =
80 "/api/users/login/server_custom_id?" +
81 `projectId=${loginProjectId}&` +
82 `publisher_project_id=${projectId}`;
83
84 const options = {
85 hostname: "login.xsolla.com",
86 port: 443,
87 path: path,
88 method: "POST",
89 headers: {
90 "Content-Type": "application/json",
91 "Content-Length": postData.length,
92 "X-Server-Authorization": serverToken,
93 },
94 };
95
96 const req = https.request(options, (res) => {
97 if (res.statusCode !== 200) {
98 reject(
99 new functions.https.HttpsError(
100 "internal",
101 "Client token not received"
102 )
103 );
104 }
105 let body = [];
106 res.on("data", (d) => {
107 body.push(d);
108 });
109 res.on("end", () => {
110 try {
111 body = JSON.parse(Buffer.concat(body).toString());
112 } catch (e) {
113 reject(
114 new functions.https.HttpsError(
115 "internal",
116 "Malformed client token response"
117 )
118 );
119 }
120 resolve({
121 "token": body.token,
122 });
123 });
124 });
125 req.on("error", (e) => {
126 reject(new functions.https.HttpsError(
127 "internal",
128 "Internal error while client token flow"
129 ));
130 });
131
132 req.write(postData);
133 req.end();
134}
- この例のように、機能を実際の使用環境に展開します。
- アプリケーションから機能を呼び出すため、クライアントサイドロジックを追加します。機能名には
getXsollaLoginToken
を指定します。パラメータを渡す必要はありません。 - アプリケーションでは、APIを操作するためのメソッドを自分で実装するか、エクソーラSDKを使用します。
PlayFabプロジェクトにクラウドスクリプトを追加する
- ユーザーJWTを受信するための機能コードを含むJSファイルを作成します。そこで:
<ProjectID>
はプロジェクトIDで、アドミンページでプロジェクト名の横に表示されます。<LoginID>
は、アドミンページを開き、「ログイン > ダッシュボード > ログインプロジェクト」セクションに移動し、ログインプロジェクト名の横にある「IDをコピー」をクリックすることで取得できるログインIDです。<OAuthClientID>
はサーバーOAuth 2.0クライアントをセットアップする時に受信したクライアントIDです。<OAuthSecretKey>
はサーバーOAuth 2.0クライアントをセットアップする時に受信した秘密鍵です。
お知らせ
既にプロジェクトでクラウドスクリプトを使用している場合は、ユーザーJWTを受信するための機能をコードの最後に追加してください。
Copy
- javascript
1handlers.GetXsollaLoginToken = function (args) {
2
3 // TODO replace with production credentials
4 const projectId = <ProjectID>;
5 const loginProjectId = "<LoginID>";
6 const serverOauthClientId = <OAuthClientID>;
7 const serverOauthClientSecret = "<OAuthSecretKey>";
8
9 const getServerTokenBody =
10 "grant_type=client_credentials" +
11 `&client_secret=${serverOauthClientSecret}` +
12 `&client_id=${serverOauthClientId}`;
13
14 const serverTokenResponse = JSON.parse(
15 http.request(
16 "https://login.xsolla.com/api/oauth2/token",
17 "post",
18 getServerTokenBody,
19 "application/x-www-form-urlencoded",
20 {})
21 );
22
23 let serverToken = ""
24 if ('access_token' in serverTokenResponse) {
25 serverToken = serverTokenResponse.access_token;
26 } else {
27 return {
28 "error_message": "Server token not received"
29 }
30 }
31
32 const getUserTokenHeaders = {
33 "X-Server-Authorization": serverToken
34 }
35
36 const getUserTokenBody = JSON.stringify(
37 {
38 "server_custom_id": currentPlayerId,
39 }
40 );
41
42 const getUserTokenPath =
43 "/api/users/login/server_custom_id?" +
44 `projectId=${loginProjectId}&` +
45 `publisher_project_id=${projectId}`;
46
47 const userTokenResponse = JSON.parse(
48 http.request(
49 "https://login.xsolla.com" + getUserTokenPath,
50 "post",
51 getUserTokenBody,
52 "application/json",
53 getUserTokenHeaders)
54 );
55
56 if ('token' in userTokenResponse) {
57 return {
58 "token": userTokenResponse.token
59 }
60 } else {
61 return {
62 "error_message": "User token not received"
63 }
64 }
65}
- PlayFabプロジェクト設定に移動します。
- クラウドスクリプトファイルをアップロードします。
- 使用環境でクラウドスクリプトを実行します。
- アプリケーションから機能を呼び出すため、クライアントサイドロジックを追加します。機能名には
GetXsollaLoginToken
を指定します。パラメータを渡す必要はありません。
ユーザーJWTの受信機能の呼び出し例:
Copy
kotlin
- kotlin
- C#
- C++
1val tokenRequest = PlayFabClientModels.ExecuteCloudScriptRequest()
2tokenRequest.FunctionName = "GetXsollaLoginToken"
3val res = PlayFabClientAPI.ExecuteCloudScript(tokenRequest)
4val result = res.Result.FunctionResult as Map<*, *>
5val token = result["token"]
6val errorMessage = result["error_message"]
1var tokenRequest = new ExecuteCloudScriptRequest{
2 FunctionName = "GetXsollaLoginToken"
3};
4
5PlayFabClientAPI.ExecuteCloudScript(
6 tokenRequest,
7 scriptResult =>
8 {
9 var functionResult = scriptResult.FunctionResult as Dictionary<string, string>;
10 var token = functionResult["token"];
11 },
12 playfabError => { Debug.LogError($"GetXsollaAccessToken error: {playfabError.ErrorMessage}"); });
1void UMyClass::GetXsollaToken()
2{
3 FClientExecuteCloudScriptRequest tokenRequest;
4 tokenRequest.FunctionName = TEXT("GGetXsollaLoginToken");
5
6 UPlayFabClientAPI::FDelegateOnSuccessExecuteCloudScript onSuccess;
7 onSuccess.BindUFunction(this, "OnTokenRecieved");
8
9 UPlayFabClientAPI::FDelegateOnFailurePlayFabError onFailure;
10 onSuccess.BindUFunction(this, "OnError");
11
12 UPlayFabClientAPI::ExecuteCloudScript(tokenRequest, onSuccess, onFailure, nullptr);
13}
14
15void UMyClass::OnTokenRecieved(FClientExecuteCloudScriptResult result, UObject* customData)
16{
17 const FString& token = result.FunctionResult->GetStringField(TEXT("token"));
18
19 // do something with a token
20}
21
22void UMyClass::OnError(FPlayFabError error, UObject* customData)
23{
24 // handle errors
25}
お知らせ
この例では、PlayFab SDKのメソッドを使用してクラウドスクリプトへのリクエストを行います。PlayFab SDKをプロジェクトに追加しなくても、リクエストとレスポンスの処理を自分で実装することができます。
- アプリケーションで、APIを自分で操作するためのメソッドを実装するか、Xsolla SDKを使用します。
この記事は役に立ちましたか?
ご意見ありがとうございました!
あなたのメッセージを確認し、体験を向上させるために利用させていただきます。お役立ちリンク
最終更新日: 2025年4月2日誤字脱字などのテキストエラーを見つけましたか? テキストを選択し、Ctrl+Enterを押します。