Login with VKID🔗

Login with VKID - this is a single platform for authorization and registration of users in different services of the VK ecosystem.
Android sdk-fingerprint is incorrect
If you are using the new apk/bundle signing mechanism in GooglePlay, you should take SHA1 from GooglePlay Console of your project.
VKID min api level
VKID requires Android 5.0+ (Api level 21+).
Pre-setup🔗
Getting keys and necessary data🔗
Before start, create an application on the VK platform website. After creating the application, you will receive a unique identifier for your application (application ID) and a secure key.
Project Setup🔗
Configure your project according to the documentation VK platform.
Also, you need to add Service access key from VK platform to MRGS console of your project -> Integration -> VK access token
Adding Universal links, schemas in iOS
VKId requires that the application supports universal links and that the necessary schemes are written in the application's plist. You can see about setting up Universal links in the VK documentation above, but if necessary, you can use the MRGS domain for universal links, for this, set a task for the MRGS project, or contact us in the support chat, and we will write your bundleId in our app-site-association. Below is a code snippet for Unity to add the necessary data to the application:
[PostProcessBuild]
public static void PostProcessBuild(BuildTarget target, string path)
{
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
PlistElementArray schemesToOpen = rootDict.CreateArray("LSApplicationQueriesSchemes");
schemesToOpen.AddString("vkauthorize-silent");
// Adding URL-scheme
PlistElementArray appSchemes = rootDict.CreateArray("CFBundleURLTypes");
PlistElementDict appSchemesDict = appSchemes.AddDict();
appSchemesDict.SetString("CFBundleURLName", "");
PlistElementArray appSchemesArray = appSchemesDict.CreateArray("CFBundleURLSchemes");
appSchemesArray.AddString("vk<VKontakte_APP_ID>");
File.WriteAllText(plistPath, plist.WriteToString());
// Adding Universal Links
var projectManager = new ProjectCapabilityManager(projPath, "ios.entitlements", targetGuid: projectTarget);
projectManager.AddAssociatedDomains(new []
{
"applinks:mrgs.astrum.team" // Your domain or mrgs domain if universal links is made via mrgs
});
projectManager.WriteToFile();
}
For iOS Native:
To add schemes to LSApplicationQueriesSchemes (to open other applications) you need to:
- Open Info.plist of the project
- Add new field with name LSApplicationQueriesSchemes and type array
- Add the necessary elements to the created array(
vkauthorize-silent)
To add URL-scehem (to have ability to open application from outsiede) you need to:
- Open Info.plist
- Add string «URL types»
- Expand the first element ("Item0"), add the string "URL identifier" and add the value with the identifier of your application (for example, com.company.appname).
- Add string to the first element ("item0") in "URL types" and name it "URL Schemes" and add a new element (desired URL scheme) to "URL Schemes"
To add Universal Links capability, in the XCode project in the 'Signing & Capabilities' tab you need to add 'Universal Links'
Adding Dependencies🔗
- Add a MRGSAuthenticationVKId module
Adding to Unity project (general instruction)
- Add VK repository to your root gradle file:
allprojects {
repositories {
// others
maven { url "https://artifactory-external.vkpartner.ru/artifactory/superappkit-maven-public/" }
}
}
Step 1. Add Sources
To add MRGS to a project via the Unity Package Manager (available from Unity 2018+) simply add a scopedRegistries section to the Packages/manifest.json file by adding the following entry:
{
"dependencies": {
...
},
"scopedRegistries": [
{
"name": "MRGS",
"url": "https://mrgs-nexus.my.games/repository/mrgs-uninty-plugins/",
"scopes": [
"games.my.mrgs"
]
}
]
}
Alternatively, you can click Edit -> Project Settings -> Package Manager -> '+' in scoped registry section, and fill in the fields according to the data above.
Step 2. Add dependency
- Click
Window -> Package Manager -> select 'Packages: MyRegistries' from dropdown list, select packageMRGSAuthenticationVKIdfrom the list, then click "Install" - Import the module:
using MRGS;
- Download the latest version of the library. Unzip the archive.
- (For unitypackage integration) In Unity, click
Assets -> Import Package -> Custom Package, and select thegames.my.mrgs.authenticationvkid.unitypackagepackage from the downloaded archive. - (For tgz integration) In Unity, click
Window -> Package Manager -> '+' -> Add package from tarball, and select thegames.my.mrgs.authenticationvkid-<version> package. tgzfrom the downloaded archive. - Import the module:
using MRGS;
Adding to iOS project (general instruction)
Step 1: Add dependencies
Via Package collection
- In Xcode select File > Add Packages
- Select "+" > "Add Swift Package Collection"
- Insert URL:
https://mrgs-nexus.my.games/repository/ios-sdks/MRGSPackageCollection.json - Select module MRGSAuthenticationVKId from "MRGS Package Collection".
- Or you can select "MRGS" package from "MRGS Package Collection" (contains all mrgs modules as products) and then select "MRGS/AuthenticationVKId" product only.
Individual packages
- In Xcode select File > Add Packages
- In the search bar at the top right, paste the URL:
https://mrgs-gitea.my.games/mrgs/mrgsauthenticationvkid-ios-sdk.git - Add a module to your project
- Or you can paste the url
https://mrgs-gitea.my.games/mrgs/ios-sdks.gitto include the "MRGS" package which contains all mrgs modules as products and then select only the product" MRGS/AuthenticationVKId".
Step 2: Add support for ObjectiveC categories
- Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthenticationVKId;or#import <MRGSAuthenticationVKId/MRGSAuthenticationVKId.h>
Step 1. Add Sources
In your podfile, add sources to the top of the file:
source 'https://github.com/CocoaPods/Specs.git' # For main repo
source 'https://mrgs-gitea.my.games/mrgs/cocoapods-specs.git' # For MRGS repo
Step 2: Add dependencies
Add the latest version of MRGSAuthenticationVKId to target:
To add via subspecs:
To add via individual modules:
To add all mrgs modules:
Step 3: Install dependencies
- Run
pod install(orpod install --repo-updateif necessary) - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthenticationVKId;or#import <MRGSAuthenticationVKId/MRGSAuthenticationVKId.h>
Step 1: Add dependencies
Add the dependency to your Cartfile:
binary "https://mrgs-nexus.my.games/repository/ios-sdks/MRGSAuthenticationVKId/MRGSAuthenticationVKId.json" ~> 5.0.0
Step 2: Install dependencies
- Run
carthage update --use-xcframeworks - Add downloaded frameworks to your project (make sure "do not embed" option is enabled)
- Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthenticationVKId;or#import <MRGSAuthenticationVKId/MRGSAuthenticationVKId.h>
- Download the latest version of the library. Unzip the archive.
-
Add
MRGSAuthenticationVKId.xcframeworkfrom the downloaded archive to your project (Drag the libraries to the "Linked frameworks and Libraries" section) (Also contains MRGSAuthenticationVKId.framework for compatibility - fat framework) -
Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGSAuthenticationVKId;or#import <MRGSAuthenticationVKId/MRGSAuthenticationVKId.h> - Also, you can add the
MRGServiceKit.handmodule.modulemapfiles from the archive to your project, or specify the path to them in the project settings in theBuild Settings -> Header search pathssection. Now, instead of importing each of our frameworks separately, you can only import one header file:@import MRGServiceKit;
Adding to Android project
- Add VK repository to your root gradle file:
allprojects {
repositories {
// others
maven { url "https://artifactory-external.vkpartner.ru/artifactory/superappkit-maven-public/" }
}
}
- Add dependencies to your Application gradle file.
Copy the MRGSAuthenticationVKId.aar file to the libs directory of your project. Add the necessary dependencies to the build file.gradle
VK authentication
VKID is not compatible with another library for authorization/registration in VK. Make sure that these two libraries do not get into the assembly at the same time, or add the following script to your mainTemplate.gradle file to exclude the old authorization option from compilation:
Configuring MRGS Parameters🔗
Pass received VK project id and VK project secret values to MRGS. While setting the MRGS SDK you need to add the appropriate setting in the parameters of the external SDK:
using MRGS;
public class MasterController : MonoBehaviour
{
void Awake()
{
// Setting up mrgs params
// ...
var modulesParams = new List<MRGSExternalSDKSettings>
modulesParams.Add(new MRGSVKIdParams("<VK_PROJECT_ID>", "<VK_PROJECT_SECRET>"))
// Setting up external sdk params
// ...
MRGService.Instance.Initialize(serviceParams, sdkParams)
}
}
@import MRGService;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Setting up mrgs params
// ...
//Setting up external sdk params
MRGSVKIdParams* vkIdLoginParams = [[MRGSVKIdParams alloc] initWithClientId:@"<VK_PROJECT_ID>" clientSecret:@"<VK_PROJECT_SECRET>"];
NSArray *externalParams = @[ ..., vkIdLoginParams];
[MRGService startWithServiceParams:<params> externalSDKParams:externalParams delegate:nil];
}
Android
VKID settings must be specified in the android resources according to documentation of VK platform.
Using MRGSAuthentication interface🔗
To work with Login with VKontakte, use the MRGSAuthenticationVKId class (MRGSVKId on Android). Please note that this class implements main interface MRGSAuthentication, that is, it has all the methods of this interface. The following describes the standard implementation of the MRGSAuthentication interface:
Delegate🔗
Before start using the API, implement and set a delegate that accepts callbacks:
// Setting
// 'this' conforms to 'IMRGSAuthenticationDelegate' protocol
MRGSAuthenticationVKId.Instance.Delegate = this;
// Implementation
// The method that is called when the user has logged out of the social network remotely, or his session has ended.
public void OnAuthenticationProviderDidLogoutUser(MRGSAuthentication provider, MRGSAuthenticationUser user){
Debug.Log("MRGSAuthentication<Unity> - OnAuthenticationProviderDidLogoutUser: " + user + "\nNetwork: " + provider.SocialId().ToString());
}
// Set
[MRGSAuthenticationVKId sharedInstance].delegate = self;
// Implementation
// The method that is called when the user remotely logs out of the social network, or his session ends.
- (void)authenticationProvider:(id<MRGSAuthentication>)provider didLogoutUser:(MRGSAuthenticationUser *)user{
NSLog(@"User did logout: \n%@", user);
}
// The method that is called when the controller needs to be displayed to authorize the user.
- (void)shouldPresentAuthorizationController:(UIViewController *)viewController {
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:viewController animated:YES completion:nil];
}
import androidx.annotation.NonNull;
import games.my.mrgs.authentication.MRGSAuthentication.OnExternalLogoutListener;
import games.my.mrgs.authentication.vkid.MRGSVKId;
// Set a listener
MRGSVKId.getInstance().setOnExternalLogoutListener(new OnExternalLogoutListener() {
@Override
public void onUserLogout(@NonNull String socialId) {
// Handel result.
}
});
Status🔗
To check login status call:
Login🔗
For regular login call one of the methods:
// If error occured, error object will be non-null
MRGSAuthenticationVKId.Instance.Login((MRGSError error) => {
if (error == null) {
// Auth success
}
});
// The response will include an object describing the user and the token, and an error object, if any.
MRGSAuthenticationVKId.Instance.Login((MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationVKId:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
// Only the object of the error will come in the answer, if it happened
[[MRGSAuthenticationVKId sharedInstance] login:^(NSError *error) {
if (!error) {
// Auth success
}
}];
// The response will contain an object describing the user and the token, and the object of the error if it happened
[[MRGSAuthenticationVKId sharedInstance] loginWithCompletionHandler:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
if (!error) {
// Auth success
}
}];
import android.app.Activity;
import androidx.annotation.NonNull;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSCredentials;
import games.my.mrgs.authentication.MRGSLoginCallback;
import games.my.mrgs.authentication.vkid.MRGSVKId;
MRGSVKId.getInstance().login(activity, new MRGSLoginCallback() {
@Override
public void onSuccess(@NonNull MRGSCredentials credentials) {
// Handle result.
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error.
}
});
Login with permissions🔗
Additional permissions can no longer be transferred from the client. Now you need to configure the necessary permissions in VK platform.
Important
Please note that the user may not give some permissions. The list of received permissions can be viewed in the credentials.accessToken.authorizedScopes field.
Authentication status🔗
To get all the information about the current authorization status, user information, authorization status, use the methods:
// The information about the user, identifier, name, email, etc.
MRGSAuthenticationVKId.Instance.GetCurrentUser((MRGSAuthenticationUser user, MRGSError error) => {
Debug.Log("MRGSAuthenticationVKId:\User: " + user + "\nError:\n" + error);
if (error == null) {
// Work with user data
}
});
// Information about the token, lifetime, etc.
MRGSAuthenticationVKId.Instance.GetAccessToken((MRGSAuthenticationAccessToken token, MRGSError error) => {
Debug.Log("MRGSAuthenticationVKId:\Token: " + token + "\nError:\n" + error);
if (error == null) {
// Work with token info
}
else if (error.Code == (int)MRGSAuthenticationErrorCode.ConnectionFailed)
{
// No network, try later
}
else
{
// Logout user from app (MRGS will logout automatically in SDK)
}
});
// An object is returned that combines information about the user (identifier, name, email, etc.) and about the token.
[[MRGSAuthenticationVKId sharedInstance] getCredentials:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
if(!error){
// Credentials data work
}
}];
// Information about the user, identifier, name, email, etc. is returned separately.
[[MRGSAuthenticationVKId sharedInstance] getCurrentUser:^(MRGSAuthenticationUser *user, NSError *error) {
NSLog(@"User - %@. Error - %@", user, error);
if(!error){
// User data work
}
}];
/ /Information about token and expiration date
[[MRGSAuthenticationVKId sharedInstance] getAccessToken:^(MRGSAuthenticationAccessToken *token, NSError *error) {
if (!error) {
NSLog(@"getAccessToken result - %@", token);
} else {
NSLog(@"getAccessToken error - %@", error);
}
}];
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAccessToken;
import games.my.mrgs.authentication.MRGSAuthentication;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.utils.optional.BiConsumer;
import games.my.mrgs.authentication.vkid.MRGSVKId;
// Returns user's information.
MRGSVKId.getInstance().getCurrentUser(new MRGSAuthentication.UserCallback() {
@Override
public void onSuccess(@NonNull final MRGSUser user) {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
// Returns authentication's information.
MRGSVKId.getInstance().getAccessToken(new BiConsumer<MRGSAccessToken, MRGSError>() {
@Override
public void accept(@Nullable MRGSAccessToken accessToken, @Nullable MRGSError error) {
if (error != null) {
Log.d("MRGSAuthentication", "AccessToken error: " + error.getErrorText());
} else {
Log.d("MRGSAuthentication", "AccessToken success: " + accessToken);
}
}
});
Logout🔗
To "log out" of a user’s account, use the method:
Getting avatar🔗
To get a user avatar with a given identifier, use the method:
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAvatarCallback;
import games.my.mrgs.authentication.vkid.MRGSVKId;
// Call #getCurrentUser() to get current user.
final MRGSUser user = ...
// Get user's avatar.
MRGSVKId.getInstance().getUserAvatar(user, new MRGSAvatarCallback() {
@Override
public void onSuccess(@NonNull Bitmap bitmap) {
// Handle result.
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error.
}
});
Created: 2022-10-04