Unity Authorization Example

Explore real-world code samples that are used to implement authorization in Unity applications running on Windows, Android, and iOS platforms

There are a number of HTTP frameworks available for Unity applications that simplify the process of working with HTTP requests. These frameworks provide developers with a convenient and efficient way to communicate with external APIs, including the HyperID REST API.

UnityWebRequest, HttpClient, RestSharp, and many others provide developers with the tools and abstractions they need to make HTTP calls. Developers can choose the method that best suits their needs and preferences.

For detailed real-world examples and information regarding HTTP requests for the HyperID REST API, please visit the Interactive Web Demo section.

However, integrating authorization web flows can be challenging. Employing a web browser becomes essential in this process.

The primary goal of this example is to provide source code demonstrating seamless interaction with an external browser for authorization.

Exploring Source Code

The source code is available on GitHub: https://github.com/HyperIDMaster/HyperID-Unity-Sandbox-Demo

All the necessary source code for authorization handling can be found in the Assets\Scripts\eHyperIdAuthTest.cs file. It contains several classes:

  • The main class, eHyperIdAuthTest, handles the core functionality and user interface interactions.

  • The eBrowser class is responsible for launching an external browser specifically for the authorization flow. It is important to note that all parameters should be URL-encoded.

  • The eRedirectUriListener class (used only for the Windows platform) is designed to listen for the redirect_uri callback, allowing the application to capture and process the necessary authentication data. It uses the HttpListener class from the System.Net framework.

  • The eConst class contains essential configuration constants, such as clientID, client_secret, and redirect_uri. Developers can register their own Client via the HyperID Developer's Portal and utilize their unique credentials for testing and integration purposes.

When using a custom clientID / clientSecret, it is important to ensure that the configured redirect URI is also registered through the HyperID Developer's Portal. This step is required to ensure successful integration.

Platform Specifics

The platform-specific code is determined by defining one of the following constants during the build process: UNITY_STANDALONE_WIN, UNITY_ANDROID, or UNITY_IOS. These constants are automatically set when the target platform for the build is selected. They are helpful for identifying the code that is specific to each platform.

The primary variation lies in how the redirect_uri callback is handled.

Windows uses the eRedirectUriListener class, described above, which is based on the HttpListener class from the System.Net framework.

Deeplink and the standard Unity OnApplicationFocus() are used on mobile platforms. If deeplink is properly configured, developers can obtain redirect_uri within the OnApplicationFocus() function using Application.absoluteURL.

Depending on the mobile platform, the deeplink registration may be different:

  • Android: unity\Assets\Plugins\Android\AndroidManifest.xml

  • iOS: unity\ProjectSettings\ProjectSettings.asset (iOSURLSchemes parameter)

Last updated