Oculus (Meta Quest)
Provide an Oculus sign-in option to enable players to authenticate using their Meta Quest accounts on virtual reality platforms.
Read time 2 minutesLast updated 15 hours ago
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.
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.
- 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 } }}
Sign in a returning player or create new player
You can use theSignInWithOculusAsync- Create a new Unity Authentication player with the Oculus credentials.
- Sign in an existing player using the Oculus credentials.
SignInWithOculusAsyncSignInWithOculusAsyncSignInWithOculusAsyncasync 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 theLinkWithOculusAsync- Sign into the cached player's account using .
SignInAnonymouslyAsync - Link the cached player's account to the Oculus account with .
LinkWithOculusAsync
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 theUnlinkOculusAsyncasync Task UnlinkOculusAsync(){ try { await AuthenticationService.Instance.UnlinkOculusAsync(); 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); }}