Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I implement UGS authentication to sign in with Steam?

Discussion in 'Authentication' started by bh4786, Sep 20, 2023.

  1. bh4786

    bh4786

    Joined:
    May 24, 2017
    Posts:
    16
    I've been researching with another coder on my team for hours and I can't find any examples on this. The documentation for this is very vague. There are some classes to reference but seeing as how this is all new to me, I have no idea how to implement them. I've seen some posts here but their implementation doesn't work with the recent updates to authentication. This is what I have so far and I'm stuck. I can't get the OnAuthCallback() method to get called. It just goes through SignInWithSteam() and stops.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using Steamworks;
    4. using Unity.Services.Core;
    5. using Unity.Services.Authentication;
    6. using System.Threading.Tasks;
    7.  
    8. public class InitialSteamSignIn : MonoBehaviour
    9. {
    10.     private Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;
    11.     private HAuthTicket m_AuthTicket;
    12.     private string m_SessionTicket;
    13.  
    14.     private async void Start()
    15.     {
    16.         await UnityServices.InitializeAsync();
    17.         //Debug.Log(UnityServices.State);
    18.         SignInWithSteam();
    19.     }
    20.     private void SignInWithSteam()
    21.     {
    22.         m_AuthTicketForWebApiResponseCallback = Callback<GetTicketForWebApiResponse_t>.Create(OnAuthCallback);
    23.         m_AuthTicket = SteamUser.GetAuthTicketForWebApi("unityauthenticationservice");
    24.     }
    25.  
    26.     private void OnAuthCallback(GetTicketForWebApiResponse_t callback)
    27.     {
    28.         m_SessionTicket = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty);
    29.         m_AuthTicketForWebApiResponseCallback.Dispose();
    30.         m_AuthTicketForWebApiResponseCallback = null;
    31.         Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket);
    32.  
    33.         //LogInToSteam(??);
    34.     }
    35.     private async void LogInToSteam(string ticket, string identity)
    36.     {
    37.         await SignInWithSteamAsync(ticket, identity);
    38.     }
    39.  
    40.     private async Task SignInWithSteamAsync(string ticket, string identity)
    41.     {
    42.         try
    43.         {
    44.             await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity);
    45.             Debug.Log("SignIn is successful.");
    46.         }
    47.         catch (AuthenticationException ex)
    48.         {
    49.             // Compare error code to AuthenticationErrorCodes
    50.             // Notify the player with the proper error message
    51.             Debug.LogException(ex);
    52.         }
    53.         catch (RequestFailedException ex)
    54.         {
    55.             // Compare error code to CommonErrorCodes
    56.             // Notify the player with the proper error message
    57.             Debug.LogException(ex);
    58.         }
    59.     }
    60. }
     
    Filip8429 and VolodymyrKovtoniuk like this.
  2. mike73miller

    mike73miller

    Joined:
    Oct 27, 2019
    Posts:
    9
    The documentation on this is vague.
    here are some steps:
    1. Go into package manager and make sure you are using at least Authentication Version 3.0.0 - Aug 22, 2023
    2. Make sure the file steam_appid.txt in your assets folder has your app ID.
    3. Open steammanager.cs and replace 480 with your game's app ID.
    4. Under project settings, services, Authentication, enter your App ID, and API Key.
    5. Wishlist my game on steam: https://store.steampowered.com/app/2589370/Alien_Battle_Champions/

    Attach This code to a game object on your title screen:
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Services.Authentication;
    3. using Unity.Services.Core;
    4. using System;
    5. using Steamworks;
    6.  
    7. public class PlayerID : MonoBehaviour
    8. {
    9.     public string _playerId;
    10.     public string PlayerName;
    11.     public ulong steamID;
    12.     Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;
    13.     HAuthTicket m_AuthTicket;
    14.     private string m_SessionTicket;
    15.     public GameObject SteamNotice;
    16.     public GameObject StartGame;
    17.     private void Awake()
    18.     {
    19.         GameObject[] objs = GameObject.FindGameObjectsWithTag("PlayerID");
    20.         if (objs.Length > 1) { Destroy(this.gameObject); }
    21.         DontDestroyOnLoad(this.gameObject);
    22.     }
    23.     private void Start()
    24.     {
    25.         if (SteamManager.Initialized)
    26.         {
    27.             PlayerName = SteamFriends.GetPersonaName();
    28.             steamID = SteamUser.GetSteamID().m_SteamID;
    29.             Debug.Log(PlayerName);
    30.             Debug.Log(steamID);
    31.             StartUnityServices();
    32.             SignInWithSteam();        
    33.         }
    34.         void SignInWithSteam()
    35.         {
    36.             m_AuthTicketForWebApiResponseCallback = Callback<GetTicketForWebApiResponse_t>.Create(OnAuthCallback);
    37.             m_AuthTicket = SteamUser.GetAuthTicketForWebApi("unityauthenticationservice");        
    38.         }
    39.         void OnAuthCallback(GetTicketForWebApiResponse_t callback)
    40.         {
    41.             m_SessionTicket = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty);
    42.          
    43.             Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket);
    44.             SignInWithSteamAsync(m_SessionTicket, "unityauthenticationservice");        
    45.         }
    46.         async void SignInWithSteamAsync(string ticket,string identity)
    47.         {
    48.             try
    49.             {
    50.                 await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity);
    51.                 Debug.Log("SignIn is successful.");
    52.                 _playerId = m_SessionTicket;
    53.                 // enable start game button
    54.                 SteamNotice.SetActive(false);
    55.                 StartGame.SetActive(true);  
    56.             }
    57.             catch (AuthenticationException ex)
    58.             {
    59.                 Debug.LogException(ex);
    60.             }
    61.             catch (RequestFailedException ex)
    62.             {
    63.                 Debug.LogException(ex);
    64.             }
    65.         }
    66.         async void StartUnityServices()
    67.         {
    68.             await UnityServices.InitializeAsync();  
    69.         }
    70.     }
    71. }
     
  3. Svartskogen

    Svartskogen

    Joined:
    Apr 17, 2017
    Posts:
    9
    I'm having the same issue, before I was just doing

    AuthenticationService.Instance.SignInWithSteamAsync(sessionTicket)

    with the ticket I get from

    SteamUser.GetAuthSessionTicketAsync();


    and its working fine, but now SignInWithSteamAsync says its deprecated that way and asks me for a identity parameter, what should that identity parameter should be exactly?

    I see you guys are using
    SteamUser.GetAuthTicketForWebApi
    , but I don't have that method, I'm also using Steamworks, what am I missing?
     
  4. erickb_unity

    erickb_unity

    Unity Technologies

    Joined:
    Sep 1, 2021
    Posts:
    90
  5. Svartskogen

    Svartskogen

    Joined:
    Apr 17, 2017
    Posts:
    9
    Ah okay my bad, I'm actually using Facepunch.Steamworks.

    I wonder what should I do in my case, maybe I can consider switching to Steamworks.NET, since from what I see Facepunch.Steamworks is lagging a bit behind on API updates, and seems everyone is using Steamworks.NET nowdays
     
  6. Scoppex

    Scoppex

    Joined:
    Apr 13, 2021
    Posts:
    9
  7. erickb_unity

    erickb_unity

    Unity Technologies

    Joined:
    Sep 1, 2021
    Posts:
    90
    That is likely related to your steam project setup.
    Are other calls to the steamworks sdk working correctly?
    You can look at the steamworks documentation on properly setting up your project.