Oculus (Meta Quest)
Minimum SDK version: 2.3.1
This article guides you through the following scenarios in setting up authentication for players in your game with an Oculus account:
- Set up an Oculus sign-in
- Sign in a returning user or create new user.
- Updating a user from an anonymous sign-in to a platform sign-in via your custom ID provider.
To provide an Oculus sign-in option for the players in your game, create an app in the Oculus Developer Dashboard and take note of your App ID and App Secret, as well as the nonce and userId.
Refer to the following section to add the package:
Set up an Oculus sign-in
To set up Oculus as an ID Provider for Unity Authentication:
- In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
- Set ID Providers to Oculus, then click Add.
- Enter the app ID in the App ID text field.
- Enter the app secret in the App Secret text field.
You can use the code below as an example of how to retrieve the logged in Oculus user and generate a nonce:
Note: The referenced package is not developed, owned, or operated by Unity. Please refer to our best practices for working with non-Unity packages.
- Put on the headset, and go to Settings > System > Developer.
- Enable the USB Connection Dialog option.
- Connect the headset to the computer using a USB-C cable.
- There should be a prompt that appears asking for data access, click Allow.
- To make sure the device connection is successful, open your Unity project and navigate to File > Build Settings. Within the Platform list, select Android and click Switch Platform.
- In the Run Device list, select the Oculus headset. If the headset is not within the list, click Refresh to refresh the options.
- Set up and install the Oculus Integration package located here. Alternatively, you can find the package from the Oculus Developer Center.
- Once imported in your project, from the Editor’s menu bar, go to Oculus > Platform > Edit Settings. You'll need to enter in your Oculus App ID.
using UnityEngine;
using Oculus.Platform;
using Oculus.Platform.Models;
public class OculusAuth : MonoBehaviour
{
private string userId;
private void Start()
{
Core.AsyncInitialize().OnComplete(OnInitializationCallback);
}
private void OnInitializationCallback(Message<PlatformInitialize> msg)
{
if (msg.IsError)
{
Debug.LogErrorFormat("Oculus: Error during initialization. Error Message: {0}",
msg.GetError().Message);
}
else
{
Entitlements.IsUserEntitledToApplication().OnComplete(OnIsEntitledCallback);
}
}
private void OnIsEntitledCallback(Message msg)
{
if (msg.IsError)
{
Debug.LogErrorFormat("Oculus: Error verifying the user is entitled to the application. Error Message: {0}",
msg.GetError().Message);
}
else
{
GetLoggedInUser();
}
}
private void GetLoggedInUser()
{
Users.GetLoggedInUser().OnComplete(OnLoggedInUserCallback);
}
private void OnLoggedInUserCallback(Message<User> msg)
{
if (msg.IsError)
{
Debug.LogErrorFormat("Oculus: Error getting logged in user. Error Message: {0}",
msg.GetError().Message);
}
else
{
userId = msg.Data.ID.ToString(); // do not use msg.Data.OculusID;
GetUserProof();
}
}
private void GetUserProof()
{
Users.GetUserProof().OnComplete(OnUserProofCallback);
}
private void OnUserProofCallback(Message<UserProof> msg)
{
if (msg.IsError)
{
Debug.LogErrorFormat("Oculus: Error getting user proof. Error Message: {0}",
msg.GetError().Message);
}
else
{
string oculusNonce = msg.Data.Value;
// Authentication can be performed here
}
}
}
Note: Your Oculus headset must be put into developer mode. This can be accomplished by signing into the developer account you would like to use for development (you can create an Oculus developer account here).
Sign in a returning player or create new player
You can use the SignInWithOculusAsync
method to either:
- Create a new Unity Authentication player with the Oculus credentials.
- Sign in an existing player using the Oculus credentials.
If no Unity Authentication player in your project is associated with the credentials, SignInWithOculusAsync
creates a new player. If a Unity Authentication player in your project is associated with the credentials, SignInWithOculusAsync
signs into that player account.
This function doesn’t consider the cached player, and SignInWithOculusAsync
replaces the cached player.
async Task SignInWithOculusAsync(string nonce, string userId)
{
try
{
await AuthenticationService.Instance.SignInWithOculusAsync(nonce, userId);
Debug.Log(“SignIn is successful.”);
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}
Updating a player from anonymous to an Oculus account
After you’ve set up anonymous authentication, if the player wants to upgrade from being anonymous to creating an Oculus account and sign in using an Oculus account, the game should prompt the player to trigger the Oculus login and get the session ticket from Oculus. Then, call the LinkWithOculusAsync
API to link the player to the Oculus session ticket.
If a cached player exists on the SDK, you can link the cached player to the Oculus Account.
- Sign into the cached player's account using
SignInAnonymouslyAsync
. - Link the cached player's account to the Oculus account with
LinkWithOculusAsync
.
For more information about cached players, refer to Sign In a Cached User.
async Task LinkWithOculusAsync(string nonce, string userId)
{
try
{
await AuthenticationService.Instance.LinkWithOculusAsync(nonce, userId);
Debug.Log(“Link is successful.”);
}
catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
{
// Prompt the player with an error message.
Debug.LogError(“This user is already linked with another account. Log in instead.”);
}
catch (Exception ex)
{
Debug.LogError(“Link failed.”);
Debug.LogException(ex);
}
}
Unlink Oculus account
Use the UnlinkOculusAsync
API so your players can unlink their Oculus account. Once unlinked, if their account isn’t linked to any additional identity, it transitions to an anonymous account.
async Task UnlinkOculusAsync(string idToken)
{
try
{
await AuthenticationService.Instance.UnlinkOculusAsync(idToken);
Debug.Log("Unlink is successful.");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
}