Programing

Google Play 게임즈 서비스에 대한 서버 측 액세스 사용 설정 (C#)

Medeev 2023. 11. 2. 20:52

https://developers.google.com/games/services/android/offline-access?hl=ko

 

Google Play 게임즈 서비스에 대한 서버 측 액세스 사용 설정  |  Play 게임 서비스  |  Google for Develo

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 의견 보내기 Google Play 게임즈 서비스에 대한 서버 측 액세스 사용 설정 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘

developers.google.com

 

안드로이드 앱에서 로그인후 받은 서버인증용 일회용 토큰을 백앤드 서버로 보내고 서버는 그것을 구글에 질의해 인증이 된것임을 확인해야 한다.

 

공식홈에는 위와같은 JAVA 예제가 있으나 C#서버 예제가 없다.

C# 용 예제는 스택오버플로우에서 검색해서 찾았고 

https://stackoverflow.com/questions/56786698/google-playgamesapi-how-to-validate-serverauthcode-in-c-sharp

 

Google PlayGamesAPI: How to validate ServerAuthCode in C#

I have developed an Android game which successfully gets a ServerAuthCode from the Google Play API. I want to send this ServerAuthCode to my custom game server, which I have wrote in C# and validat...

stackoverflow.com

 

여기에도 남겨본다.

 

 C# NugetPackage 

 -  https://www.nuget.org/packages/Google.Apis.AndroidPublisher.v3/

 

Google.Apis.AndroidPublisher.v3 1.63.0.3225

Google APIs Client Library for working with Androidpublisher v3. Supported Platforms: - .NET Framework 4.5+ - .NET Standard 1.3 and .NET Standard 2.0; providing .NET Core support. Incompatible platforms: - .NET Framework < 4.5 - Silverlight - UWP (will bui

www.nuget.org

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Requests;
using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace GoogleApiTest
{
    // Source: https://developers.google.com/identity/sign-in/android/offline-access
    class Program
    {
        static void Main(string[] args)
        {
            var authCode = "YOUR_FRESH_SERVER_AUTH_CODE";

            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"client_secret.json");
            var config = File.ReadAllText(path, Encoding.UTF8);

            GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(new FileStream(path, FileMode.Open));

            var request = new AuthorizationCodeTokenRequest()
            {
                ClientId = clientSecrets.Secrets.ClientId,
                ClientSecret = clientSecrets.Secrets.ClientSecret,
                RedirectUri = "",
                Code = authCode,
                GrantType = "authorization_code"
            };

            var tokenResponse = request.ExecuteAsync(new System.Net.Http.HttpClient(), "https://www.googleapis.com/oauth2/v4/token", new System.Threading.CancellationToken(), Google.Apis.Util.SystemClock.Default).GetAwaiter().GetResult();

            Console.ReadLine();
        }
    }
}