自动登录外部网站
外部网站自动登录 允许已经登录外部网站(例如,Hive Community)的用户通过 Crossplay Launcher 启动 PC 游戏并自动登录——无需任何额外的登录步骤。
基本流程和实现¶
通过外部网站的跨平台启动器自动登录游戏 基本操作流程如下:
- 登录到支持“游戏外自动登录”的网站,例如 Hive Community。
- 完成登录后,使用自动登录外部网站的令牌发行 API发放令牌。
- 在已登录的网站上点击**运行游戏**,使用跨平台启动器启动 PC 游戏。
- 通过在 PC 上运行的跨平台启动器将第 2 步中发放的令牌传输到游戏。
- 在游戏中执行自动登录。
根据上述操作流程,游戏开发者可以通过以下两个步骤实现**从外部网站的自动登录**。
前提条件¶
在实现**通过跨平台启动器在外部网站上自动登录**之前,您必须准备以下内容。
- 可以使用 Web Login API 的通信环境
- 应用 Crossplay Launcher 的游戏服务环境
- SDK v4 Windows.24.4.0 或更高版本
- 外部网站的自动登录功能仅适用于应用 SDK Windows v4.24.0.0 或更高版本的游戏。
Note
- 如果您未登录网站,则在启动游戏时不会传递自动登录的令牌(web_idp_token),在这种情况下将不会执行自动登录。
-
- 有关更多信息,请参见 启动跨平台启动器时传递自动登录令牌。
1. 使用网页登录 API 获取令牌¶
当用户完成对外部网站的登录时,开发者实现了一个令牌发放,以便用户能够从登录的网站自动登录游戏。
按以下顺序请求网络登录 API 提供的三个 API,并接收 web_idp_token
令牌作为响应。
-
调用登录页面 API 请求
- 在请求后获取
state
值作为响应。
- 在请求后获取
-
验证登录并检索用户信息 API 请求
- 在请求后获得
enc_idp
值作为响应。
Warning
- 由于
enc_idp
是一个唯一值,请注意出于安全原因不要将其暴露给外部。(调用API时,服务器到服务器的通信是强制的。)
- 在请求后获得
-
从外部网站发放自动登录令牌 API 请求
- 请求后,作为响应发放一个
web_idp_token
令牌。
Note
web_idp_token
令牌随后通过跨平台启动器传递给游戏。web_idp_token
令牌是一次性值,每次运行游戏时都必须重新获取。
- 请求后,作为响应发放一个
2. 通过跨平台启动器将令牌传递给游戏¶
当用户从外部网站运行带有跨平台启动器的PC游戏时,用户已登录,开发者将编码并传递在上一步中发出的令牌web_idp_token
给游戏,以启用自动登录。
通过将令牌传递给游戏来完成自动登录的过程如下:
- 编码发出的令牌
- 配置自动登录的URI,包括编码的令牌
- 将编码的令牌包含在游戏自动安装URI或游戏执行URI中
- 实现JavaScript代码,包括自动登录的URI
- 当用户使用跨平台启动器运行PC游戏时,跨平台启动器和游戏执行将使用JavaScript代码中配置的URI完成,自动登录也将完成
Warning
如果您未登录外部网站,则无法将 web_idp_token
传递给游戏执行 URI,因此不会执行自动登录。
关于上述过程,请参考使用跨平台启动器服务游戏的实施指南。
- 为应用服务配置网页 > 一起安装 Crossplay 启动器和应用
- 为应用服务配置网页 > 运行 Crossplay 启动器
- 为应用服务配置网页 > 实现并运行 JavaScript 代码 以运行 Crossplay 启动器
配置自动登录的 URI¶
自动登录的URI通过在先前获得的URI中包含一个编码的令牌进行配置,具体取决于用户的PC上是否安装了Crossplay Launcher。
如果未安装跨平台启动器¶
在游戏自动安装URI中包含编码的令牌。
如果用户在未安装Crossplay的情况下点击Crossplay Launcher安装程序的下载按钮,已实现的JavaScript代码将把**游戏自动安装URI**复制到用户的PC剪贴板,已安装的Crossplay Launcher将搜索**游戏自动安装URI**以在PC上安装和运行游戏。因此,如果未安装Crossplay Launcher,则必须在Crossplay Launcher将要搜索的**游戏自动安装URI**中包含编码的令牌。
有关如何在未安装跨平台启动器时进行实现的更多信息,请参见跨平台启动器实施指南。
这是一个带有编码令牌的**游戏自动安装URI**示例:
hivelauncher:?app_id={AppID encoding result of the logged-in game}&start_point=9&web_idp_token={web_idp_toekn encoding result} {The entire left URI is sha1 encrypted}
# Example
hivelauncher:?app_id=com.com2us.hivesdk.windows.microsoftstore.global.normal&start_point=9&web_idp_token=eb5605514b44d7ae07f93d3921f202d0a7810eda8c343e42877b23a8a2bb5675462ae3f44c95ec59b0bdaa2ff8b28c4a971acd9439e98ef29f1c7204c230f782 9e93d5396ce59f5eaf3c3ee24a1ccda3e008364b
如果安装了跨平台启动器¶
在游戏启动 URI中包含编码的令牌。
如果用户在安装了跨平台启动器的情况下点击“启动游戏”按钮,跨平台启动器将在使用您之前实现的JavaScript代码启动**游戏启动URI**后自动安装并启动游戏。因此,如果安装了跨平台启动器,您必须在**游戏启动URI**中包含编码的令牌,以便立即安装和启动游戏。
有关如何在安装跨平台启动器时进行实现的更多信息,请参阅跨平台启动器实施指南。
这是一个带有编码令牌的**游戏启动URI**的示例:
hivelauncher:?app_id={Encoded AppID of the logged-in game}&start_point=9&web_idp_token={Encoded web_idp_toekn}
# Example
hivelauncher:?app_id=com.com2us.hivesdk.windows.microsoftstore.global.normal&start_point=9&web_idp_token=8549cd7cf3ae7d8cab788abbb3bbf4c2306e38b7d442f02214b492c98f1c05bfff7466a64ad1a3704c962d1aec0268874830eac11455c0c562aed3c0c3505a1f
跨平台启动器执行JavaScript代码实现¶
在《跨平台启动器执行JavaScript代码》中,跨平台启动器和游戏使用上述配置的URI执行。此时,来自网页登录API的web_idp_token
令牌被传递给游戏,因此可以完成通过跨平台启动器执行的游戏的自动登录。
跨平台启动器执行JavaScript代码实现 示例如下。
// Set the AppID of the logged-in game.
var appid = "로그인한 게임의 AppId 세팀함";
// When installing the launcher, set the URI for running the game.
var url_play_game = "hivelauncher:?app_id=" + encodeURIComponent(appid) + "&start_point=9";
// When not installing the launcher, set the URI to copy to the clipboard.
var url_play_clipbord = "hivelauncher:?app_id=" + encodeURIComponent(appid) + "&start_point=9";
// Implement the API call logic separately to obtain web_idp_token.
// At this time, call the internal path with ajax and call the API on the server side. (Required for security)
// Information about the API can be found in the API call under “1. Issuing tokens using the web login API” above.
// Set the web_idp_token value obtained as a result of the API call.
var web_idp_token = "API를 통해 획득 한 토큰 값 세팅함";
// Apply the web_idp_token to the URI when it is successfully obtained.
if(web_idp_token != ""){
// When the launcher is installed, add the token value for automatic login to the URI for running the game.
url_play_game += ("&web_idp_token=" + encodeURIComponent(web_idp_token));
// When the launcher is not installed, add the token value for automatic login to the URI to be copied to the clipboard.
url_play_clipbord += ("&web_idp_token=" + encodeURIComponent(web_idp_token));
// Separately implement and apply the sha1 encryption function with a space.
url_play_clipbord += (" " + sha1(url_play_clipbord));
}
// Information on this function can be found in the “Implementation Guide for Serving Games with Crossplay Launcher” above.
window.customProtocolCheck(
url_play_game, // Pass the URI for running the game set above.
function (e) {
// Add processing when Crossplay Launcher execution fails.
// ex. This is an example of necessary actions.
// Separately implement and apply the function to copy to the clipboard.
copyClipboard(url_play_clipbord);.
},
function (e) {
// Add any necessary actions when Crossplay Launcher execution succeeds.
},
2500, function (e) {
// Add processing when there is no response for more than 2500ms after Crossplay Launcher execution request.
);
有关实施和运行Javascript代码的更多信息,请参阅跨平台启动器实施指南。