TikTok - hippogamesunity/SimpleSignIn GitHub Wiki
Welcome to Simple TikTok Sign-In wiki!
The asset provides TikTok sign-in with OAuth 2.0 for Android, iOS, Windows, macOS, Universal Windows Platform (UWP) and WebGL apps made with Unity. You can also get access tokens to make REST API calls.
Benefits
- Cross-platform user auth for cross-platform games and apps
- No plugins, no 3rd party libs, no dependencies
- No impact to build size
- Get access tokens to make API calls
- More security for client-server apps (get an access token on a client, get all user data on a server to avoid tampering)
- SFSafariViewController is used on iOS (required by App Store review)
- Deep linking for Windows (UNITY_STANDALONE_WIN)
Terminology
- Please visit Terminology section
Understanding how it works
-
Generic workflow (for platforms that support deep linking):
- Your app navigates users to TikTok
Authorization Endpointusing a default web browser - Users perform sign-in
- TikTok
Authorization Endpointredirects users toRedirect URI(this can be a deep link when possible) and provides an authorizationcodeto the app (as URI parameters) - The app is activated and obtains
code - The app exchanges
codeforaccess token - The app requests user info with
access token(name, avatar and other data according access scopes defined)
- Your app navigates users to TikTok
-
Deep linking for Android and iOS:
Redirect URIis a deep link which activates the app and providescodein URI parameters
-
Loopback flow for Editor and Desktop:
Redirect URIishttp://localhost:PORT/with a random unused port- The app listens to localhost using
System.Net.HttpListener - The app obtains
codeand asks a user to close the browser tab and to return to the app - Further workflow is the same (exchanging
codeforaccess token, requesting user data)
-
Middleware flow for WebGL:
OAuth RedirecttoAuthorization Middlewareis used to temporary savecode- The app obtains
codefromAuthorization Middlewarewith a POST request - Further workflow is the same (exchanging
codeforaccess token, requesting user data)
-
For Windows (Standalone), Universal Windows Platform (UWP) and macOS (OSX):
- TikTok doesn't support deep links for these platforms (even if they support deep linking)
Desktopapp type should be used by design (with Loopback flow)- As a workaround, we can use
Webapp type and Authorization Middleware to activate your app with deep linking.
Preconditions
- For Android, iOS, macOS, Windows and UWP (platforms that support deep linking): COME UP WITH your
Custom URI scheme(orProtocol). In my example it issimple.oauth, but it can bejelly.beanormyapp(note thatCustom URI schemeis not the same as your actual package name or bundle id). For UWP, it MUST contain the period symbol.and small alphanumeric symbols only (no spaces, no undercores). - For Android, iOS, UWP: enable deep linking as described below (or refer to Unity documentation).
- For Android: create
AndroidManifest.xmlinsideAssets/Plugins/Android/, SET yourCustom URI schemeinside, like<data android:scheme="simple.oauth" />. You can useAndroidManifestExample.xmlfrom the asset as an example, just copy, rename and edit. AGAIN, DON'T FORGET TO REPLACEsimple.oauthwith yourCustom URI scheme! - For iOS and macOS: navigate to
Player Settings > Other > Configurationand add yourCustom URI schemetoSupported URL schemes. In Xcode, make sure that the URL scheme is added (Register your URL scheme). - For Universal Windows Platform: navigate to
Player Settings > Publishing Settingsand setProtocol(it MUST contain a period symbol, for examplesimple.oauth), then enableInternetClientinCapabilities. - For Windows: navigate to
Player Settingsand enableResolution and Presentation > Force Single Instanceand setOther Settings > Api Compatibility Level = .NET Framework - For Editor: Set
Allow downloads over HTTP = Always allowed(Unity 2022+)
Setup steps
- Visit TikTok for developers
- Create a new app and fill app details required
- Switch to
Sandboxto test sign-in without completing app review - Copy
Client keyandClient secret - Go to
Platformssection and enable the followingPlatforms: Web, Desktop (DO NOT ENABLE Android and iOS)- fill all information required (most fields can be set to
0for Sandbox) - for Web/Desktop: fill
Web/Desktop URL(can be0for Sandbox) - add
Login KittoProducts - set Redirect URI for Web:
https://hippogames.dev/api/oauth/redirect(you can deploy your own Authorization Middleware later) - set Redirect URI for Desktop:
http://localhost:*/ - AGAIN: Android and iOS should be unchecked even if you build for these platforms
- fill all information required (most fields can be set to
- Go to
Scopesand add extra scopes (optionally) - Go to
Sandbox settingsand add your account (you can add multiple accounts)- Note: Sandbox will work for these accounts only
- Return to Unity, select
SimpleSignIn/TikTok/Resources/TikTokAuthSettings- set
Client KeyandClient Secret(refer to step 4) - set
Custom Uri Scheme(refer to Preconditions) - set
Access Scopes(should be the same as in app configuration) - set
User Fields(they should be covered by your access scopes)
- set
Checklist
-
Custom URI schemeis picked, and it has a different value thansimple.oauth -
Custom URI schemeis set in 3 places: [1] TikTok for developers (Redirect URI), [2]Resources/TikTokAuthSettings, [3] your application manifest (AndroidManifest.xml for Android,Supported URL schemesfor iOS,Protocolfor UWP) -
Client Key,Client Secretand other settings are changed insideResources/TikTokAuthSettings
Usage
- Check our
Examplescene and C# code ofExample.cs - Create an instance of
TikTokAuth - Call
TikTokAuth.SignInorTikTokAuth.GetAccessToken - Create
OnSignInorOnGetAccessTokencallbacks - Build and test
- Write a review on the Asset Store :)
Best practices
- Call
TikTokAuth.SignInwithcaching: trueto return cachedUserInfo - Call
TikTokAuth.SignInwithcaching: falseto requestUserInfofrom TikTok - Call
TikTokAuth.GetAccessTokeninstead ofTikTokAuth.SignInif you need an access token only (and don't needUserInfo) - You can use
TikTokAuth.SavedAuthto getTokenResponseorUserInfo(don't forget to check all values for null) - Call
TikTokAuth.SignOutwhenSign outbutton is pressed (you can revoke the access token if needed) - Disable debug logs for production by setting
TikTokAuth.DebugLog = false
Next steps (optional)
- Complete app review (the most complicated step) and switch to
Production(don't forget to provide valid app details) - You can add extra access scopes in
Resources/TikTokAuthSettings(UserInfoclass should be extended as well) - If you have a backend (server), send
TokenResponseto it (to avoid tampering user data when sending from clients to your server) - For Editor and Windows (optional), you can modify
StandaloneTemplate.html(used by Loopback flow) to edit the message "Success! Please close the browser tab and return to the app." - For Windows, check Settings for Windows
- For WebGL, consider deploying your own
Authorization Middleware - You can use this asset with
asyncmethods, just refer toTikTokAuthAsync.csfor examples
Can I trust Authorization Middleware? Is it secure to use a 3rd party service?
Authorization Middlewarecan't exchangecodeforaccess tokenwithout knowing bothclient secretandcode verifier. Only the app itself can exchangecodeforaccess token.- It's recommended to deploy your own trusted
Authorization Middlewareto handle sensitive data. Please refer to Authorization Middleware article.
Notes
- Please refer to User data disclosure
- Don't use default credentials that come with the asset in production, they are for test purposes only and can be disabled/blocked
- Check Manual cancellation if needed
Known issues
- Please visit Common issues section
- TikTok no longer supports Deep Links (they now use Android App Links and iOS Universal Links, so you may see the following error:
Redirect Uri: Enter a valid Android redirect uri beginning with https://. There are 2 solutions:- Easy: to avoid complicated setup, the asset may use
Webapp type for Android and iOS and Authorization Middleware that receives oauth codes from TikTok and then activates your app with deep linking. The main issue that there may be problems with app review. SelectFlow For Android / iOS = Authorization MiddlewareinTikTokAuthSettings. - Complicated: enable
AndroidandiOSapp types, fill all required fields and then setup Android App Links and iOS Universal Links. This is the most complicated part and you'll need your own domain for this. For Android, you'll also need to modify AndroidManifest (setandroid:scheme="https"andandroid:host="yourdomain.com"). SelectFlow For Android / iOS = Android / iOSinTikTokAuthSettings.
- Easy: to avoid complicated setup, the asset may use
OAuth flow setup for Windows, UWP and macOS
- You can switch Loopback / Authorization Middleware in
TikTokAuthSettings.