question

diohshjsd avatar image
diohshjsd asked

Photon Authentication

So im using the script provided in https://learn.microsoft.com/en-us/gaming/playfab/sdks/photon/quickstart which is : using PlayFab; using PlayFab.ClientModels; using UnityEngine;

public class PlayFabAuthenticator : MonoBehaviour {

 private string _playFabPlayerIdCache;

 //Run the entire thing on awake
 public void Awake() {
     AuthenticateWithPlayFab();
 }

 /*
  * Step 1
  * We authenticate current PlayFab user normally.
  * In this case we use LoginWithCustomID API call for simplicity.
  * You can absolutely use any Login method you want.
  * We use PlayFabSettings.DeviceUniqueIdentifier as our custom ID.
  * We pass RequestPhotonToken as a callback to be our next step, if
  * authentication was successful.
  */
 private void AuthenticateWithPlayFab(){
     LogMessage("PlayFab authenticating using Custom ID...");

     PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
     {
         CreateAccount = true,
         CustomId = PlayFabSettings.DeviceUniqueIdentifier
     }, RequestPhotonToken, OnPlayFabError);
 }

 /*
 * Step 2
 * We request Photon authentication token from PlayFab.
 * This is a crucial step, because Photon uses different authentication tokens
 * than PlayFab. Thus, you cannot directly use PlayFab SessionTicket and
 * you need to explicitly request a token. This API call requires you to
 * pass Photon App ID. App ID may be hard coded, but, in this example,
 * We are accessing it using convenient static field on PhotonNetwork class
 * We pass in AuthenticateWithPhoton as a callback to be our next step, if
 * we have acquired token successfully
 */
 private void RequestPhotonToken(LoginResult obj) {
     LogMessage("PlayFab authenticated. Requesting photon token...");
     //We can player PlayFabId. This will come in handy during next step
     _playFabPlayerIdCache = obj.PlayFabId;

     PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
     {
         PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppID
     }, AuthenticateWithPhoton, OnPlayFabError);
 }

 /*
  * Step 3
  * This is the final and the simplest step. We create new AuthenticationValues instance.
  * This class describes how to authenticate a players inside Photon environment.
  */
 private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult obj) {
     LogMessage("Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");

     //We set AuthType to custom, meaning we bring our own, PlayFab authentication procedure.
     var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
     //We add "username" parameter. Do not let it confuse you: PlayFab is expecting this parameter to contain player PlayFab ID (!) and not username.
     customAuth.AddAuthParameter("username", _playFabPlayerIdCache);    // expected by PlayFab custom auth service

     //We add "token" parameter. PlayFab expects it to contain Photon Authentication Token issues to your during previous step.
     customAuth.AddAuthParameter("token", obj.PhotonCustomAuthenticationToken);

     //We finally tell Photon to use this authentication parameters throughout the entire application.
     PhotonNetwork.AuthValues = customAuth;
 }

 private void OnPlayFabError(PlayFabError obj) {
     LogMessage(obj.GenerateErrorReport());
 }

 public void LogMessage(string message) {
     Debug.Log("PlayFab + Photon Example: " + message);
 }

}

and im getting a few errors such as (72,9): error CS0103: The name 'PhotonNetwork' does not exist in the current context (64,64): error CS0103: The name 'CustomAuthenticationType' does not exist in the current context (64,30): error CS0246: The type or namespace name 'AuthenticationValues' could not be found (are you missing a using directive or an assembly reference?) (51,35): error CS0103: The name 'PhotonNetwork' does not exist in the current context

any help ?

photonAuthentication
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Simon Cui avatar image
Simon Cui answered

Hi, I tested your codes and it worked well in my side. May I suggest that you checking those steps following the document: Photon quickstart - PlayFab | Microsoft Learn? Those error messages that you mentioned in your description are related to the Photon Unity Networking Classic - FREE | Network | Unity Asset Store . You may double check if you imported the lates version of it.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

diohshjsd avatar image
diohshjsd answered

here is a reupload of the script

using PlayFab; using PlayFab.ClientModels; using UnityEngine;

public class PlayFabAuthenticator : MonoBehaviour {

 private string _playFabPlayerIdCache;

 //Run the entire thing on awake
 public void Awake() {
     AuthenticateWithPlayFab();
 }

 /*
  * Step 1
  * We authenticate current PlayFab user normally.
  * In this case we use LoginWithCustomID API call for simplicity.
  * You can absolutely use any Login method you want.
  * We use PlayFabSettings.DeviceUniqueIdentifier as our custom ID.
  * We pass RequestPhotonToken as a callback to be our next step, if
  * authentication was successful.
  */
 private void AuthenticateWithPlayFab(){
     LogMessage("PlayFab authenticating using Custom ID...");

     PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
     {
         CreateAccount = true,
         CustomId = PlayFabSettings.DeviceUniqueIdentifier
     }, RequestPhotonToken, OnPlayFabError);
 }

 /*
 * Step 2
 * We request Photon authentication token from PlayFab.
 * This is a crucial step, because Photon uses different authentication tokens
 * than PlayFab. Thus, you cannot directly use PlayFab SessionTicket and
 * you need to explicitly request a token. This API call requires you to
 * pass Photon App ID. App ID may be hard coded, but, in this example,
 * We are accessing it using convenient static field on PhotonNetwork class
 * We pass in AuthenticateWithPhoton as a callback to be our next step, if
 * we have acquired token successfully
 */
 private void RequestPhotonToken(LoginResult obj) {
     LogMessage("PlayFab authenticated. Requesting photon token...");
     //We can player PlayFabId. This will come in handy during next step
     _playFabPlayerIdCache = obj.PlayFabId;

     PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
     {
         PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppID
     }, AuthenticateWithPhoton, OnPlayFabError);
 }

 /*
  * Step 3
  * This is the final and the simplest step. We create new AuthenticationValues instance.
  * This class describes how to authenticate a players inside Photon environment.
  */
 private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult obj) {
     LogMessage("Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");

     //We set AuthType to custom, meaning we bring our own, PlayFab authentication procedure.
     var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
     //We add "username" parameter. Do not let it confuse you: PlayFab is expecting this parameter to contain player PlayFab ID (!) and not username.
     customAuth.AddAuthParameter("username", _playFabPlayerIdCache);    // expected by PlayFab custom auth service

     //We add "token" parameter. PlayFab expects it to contain Photon Authentication Token issues to your during previous step.
     customAuth.AddAuthParameter("token", obj.PhotonCustomAuthenticationToken);

     //We finally tell Photon to use this authentication parameters throughout the entire application.
     PhotonNetwork.AuthValues = customAuth;
 }

 private void OnPlayFabError(PlayFabError obj) {
     LogMessage(obj.GenerateErrorReport());
 }

 public void LogMessage(string message) {
     Debug.Log("PlayFab + Photon Example: " + message);
 }

}

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.