Login with Amazon🔗

Login with Amazon makes it easy for users to access your apps and websites. Signing in with Amazon protects your customer information using the user authentication system from Amazon. Signing in through Amazon is based on OAuth 2.0, which is widely used for authorized user exchange between sites.
Important
Note that this submodule depends on the native sdk Amazon, and is fully compatible with it. That is, if you are currently working with native sdk, that is when you switch to MRGS, we automatically raise the saved session
Setup🔗
Obtaining keys and necessary data🔗
Before begin, Need to set up a developer account on the Amazon website and get an API key for the application. Instructions for registering a project and obtaining the necessary keys can be found on the Amazon website:
For your information
On this step all you need is to get API Key. How to use it in the project is described below.
Project configuration🔗
In order for authorization to work correctly, you need to add some changes to the project:
Unity
On iOS you need to add some extra fields into Info.plist of the project:
- Add API Key obtained in the previous step, in the field with the key name "APIKey"
- Add URL-scheme to be able to open application from outside
amzn-<YOUR_BUNDLE_ID>
You need to add a new application opening scheme so that the browser can return the user back to your application. The URL scheme should be a string of the form amzn-<YOUR_BUNDLE_ID>, where YOUR_BUNDLE_ID is your unique application identifier(for example, if bundle_id is games.my.MRGServiceExample, then the added scheme should be of the form: amzn-games.my.MRGServiceExample). Details on adding URL schemes in the generated project are described in this article, but Unity has a simpler method for adding data to the application plist described below.
To do this, add code to the Unity post build process:
[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;
// second param - your API Key, obtained on the previous step
rootDict.SetString("APIKey", "<YOUR_API_KEY>");
// Additional URL-shemes
PlistElementArray appSchemes = rootDict.CreateArray("CFBundleURLTypes");
PlistElementDict appSchemesDict = appSchemes.AddDict();
appSchemesDict.SetString("CFBundleURLName", "");
PlistElementArray appSchemesArray = appSchemesDict.CreateArray("CFBundleURLSchemes");
appSchemesArray.AddString("amzn-<YOUR_BUNDLE_ID>"); // Where YOUR_BUNDLE_ID - your unique application id (for example games.my.mrgs.MRGServiceExample)
File.WriteAllText(plistPath, plist.WriteToString());
}
Or change the existing property adding code in the application's Info.plist, if you have one.
Android:
Save API Key obtained on the previous step in file wiht name api_key.txt(the name is important) and place it in the directory with the Android assets. This is usually the directory Assets/Plugins/Android/assets. During compilation, this file will be automatically placed into the assets directory inside the apk file.
iOS
On iOS you need to add some extra fields into Info.plist of the project:
- Add API Key obtained in the previous step, in the field with the key name "APIKey"
- Add URL-scheme to be able to open application from outside
amzn-<YOUR_BUNDLE_ID>
You need to add a new application opening scheme so that the browser can return the user back to your application. The URL scheme should be a string of the form ** amzn- <YOUR_BUNDLE_ID> **, where YOUR_BUNDLE_ID is your unique application identifier(for example, if bundle_id is games.my.MRGServiceExample, then the added scheme should be of the form: amzn-games.my.MRGServiceExample). Details on adding URL schemes in the generated project are described in [this article] (https://medium.com/@cafielo/custom-urlscheme-in-ios-be74a45e212b).
Using UI:
To add an API Key, you must:
- Open the projects Info.plist
- Add new field «APIKey» whith type of string
- Add API Key obtained in the previous step to the created field
To add URL-scheme you must:
- Open the projects Info.plist
- Add new field "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"
As a result you will get such a structure:

Using XML:
Add data to the Info.plist:
<key>APIKey</key>
<string>YOUR_API_KEY</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>amzn-<YOUR_BUNDLE_ID></string>
</array>
</dict>
</array>
Note: for URL schemes, if you already have such a structure in a plist with other schemes, then you can simply add one more element to the scheme array (CFBundleURLSchemes)
Android
In the project:
Save API Key obtained on the previous step in file wiht name api_key.txt(the name is important) and place it in the assets directory. This is usually the directory app/src/main/assets. During compilation, this file will be automatically placed into the assets directory inside the apk file
Add dependencies🔗
Next, you need to add the MRGS SDK and a native Amazon SDK to the project:
Unity:
Adding to the project (general instruction)
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 packageMRGSAuthenticationAmazonfrom 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.authenticationamazon.unitypackagepackage from the downloaded archive. - (For tgz integration) In Unity, click
Window -> Package Manager -> '+' -> Add package from tarball, and select thegames.my.mrgs.authenticationamazon-<version> package. tgzfrom the downloaded archive. - Import the module:
using MRGS;
iOS:
Adding to the project (general instruction)
On iOS, when using manual integration, you need to add standard tools to the project LoginWithAmazon.framework, which are in the folder Dependencies/MRGSAuthenticationDependencies/Amazon in the archive downloaded from the MRGS website. Instructions for adding 3d-party libraries to the project can be found on the Internet.
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 MRGSAuthenticationAmazon from "MRGS Package Collection".
- Or you can select "MRGS" package from "MRGS Package Collection" (contains all mrgs modules as products) and then select "MRGS/AuthenticationAmazon" 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/mrgsauthenticationamazon-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/AuthenticationAmazon".
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 MRGSAuthenticationAmazon;or#import <MRGSAuthenticationAmazon/MRGSAuthenticationAmazon.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 MRGSAuthenticationAmazon 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 MRGSAuthenticationAmazon;or#import <MRGSAuthenticationAmazon/MRGSAuthenticationAmazon.h>
Step 1: Add dependencies
Add the dependency to your Cartfile:
binary "https://mrgs-nexus.my.games/repository/ios-sdks/MRGSAuthenticationAmazon/MRGSAuthenticationAmazon.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 MRGSAuthenticationAmazon;or#import <MRGSAuthenticationAmazon/MRGSAuthenticationAmazon.h>
- Download the latest version of the library. Unzip the archive.
-
Add
MRGSAuthenticationAmazon.xcframeworkfrom the downloaded archive to your project (Drag the libraries to the "Linked frameworks and Libraries" section) (Also contains MRGSAuthenticationAmazon.framework for compatibility - fat framework) -
Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGSAuthenticationAmazon;or#import <MRGSAuthenticationAmazon/MRGSAuthenticationAmazon.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;
Android:
Adding to the project
Add a dependency in build.gradle file:
Copy the MRGSAuthentication.aar and the MRGSAuthenticationAmazon.aar files into the libs directory of your project. Add the dependencies into the build.gradle file.
Adding login button🔗
Add 'Login with amazon' button, according to Amazon button guidelines.
Using MRGSAuthentication interface🔗
To work with Login with Amazon use class MRGSAuthenticationAmazon. Please note that this class implements main interface MRGSAuthentication, so it includes the methods of this interface. The following describes the standard implementation of the MRGSAuthentication interface:
Delegate🔗
Before start working with the API, Need to implement and assign a delegate that accepts callbacks:
// Setting
// 'this' conforms to 'IMRGSAuthenticationDelegate' protocol
MRGSAuthenticationAmazon.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());
}
//Assignment
[MRGSAuthenticationAmazon sharedInstance].delegate = self;
//Implementation
//This method will be called when user has logged out outside application or access token has expired.
- (void)authenticationProvider:(id<MRGSAuthentication>)provider didLogoutUser:(MRGSAuthenticationUser *)user{
NSLog(@"User did logout: \n%@", user);
}
//The method that is called if it is necessary to display the controller 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;
import games.my.mrgs.authentication.amazon.MRGSAmazon;
// Set a listener
MRGSAmazon.getInstance().setOnExternalLogoutListener(new MRGSAuthentication.OnExternalLogoutListener() {
@Override
public void onUserLogout(@NonNull String socialId) {
// Handel result.
}
});
Status🔗
Then, need to check the authorization status (whether the user is logged in):
Login🔗
For regular login use one of the methods:
// If error occured, error object will be non-null
MRGSAuthenticationAmazon.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.
MRGSAuthenticationAmazon.Instance.Login((MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationAmazon:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
// Only the object of the error will come in the answer, if it happened
[[MRGSAuthenticationAmazon 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
[[MRGSAuthenticationAmazon 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.amazon.MRGSAmazon;
MRGSAmazon.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🔗
You can also request additional data from user:
- Profile - Name and user email
- Postal code - Zip code
Enum description:
Call login method and pass array with persmissions to request additional data
var scopes = new List<string>();
scopes.Add(MRGSAuthenticationAmazon.Scopes.Profile);
scopes.Add(MRGSAuthenticationAmazon.Scopes.PostalCode);
MRGSAuthenticationAmazon.Instance.Login(scopes, (MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationAmazon:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
import android.app.Activity;
import androidx.annotation.NonNull;
import java.util.Arrays;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSCredentials;
import games.my.mrgs.authentication.MRGSLoginCallback;
import games.my.mrgs.authentication.amazon.MRGSAmazon;
MRGSAmazon.getInstance().login(activity, Arrays.asList("profile", "postal_code"), new MRGSLoginCallback() {
@Override
public void onSuccess(@NonNull MRGSCredentials credentials) {
// Handle result
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error.
}
});
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.
MRGSAuthenticationAmazon.Instance.GetCurrentUser((MRGSAuthenticationUser user, MRGSError error) => {
Debug.Log("MRGSAuthenticationAmazon:\User: " + user + "\nError:\n" + error);
if (error == null) {
// Work with user data
}
});
// Information about the token, lifetime, etc.
MRGSAuthenticationAmazon.Instance.GetAccessToken((MRGSAuthenticationAccessToken token, MRGSError error) => {
Debug.Log("MRGSAuthenticationAmazon:\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.
[[MRGSAuthenticationAmazon 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.
[[MRGSAuthenticationAmazon sharedInstance] getCurrentUser:^(MRGSAuthenticationUser *user, NSError *error) {
NSLog(@"User - %@. Error - %@", user, error);
if(!error){
// User data work
}
}];
//Information about token and expiration date
[[MRGSAuthenticationAmazon 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.authentication.amazon.MRGSAmazon;
import games.my.mrgs.utils.optional.BiConsumer;
// Returns user's information.
MRGSAmazon.getInstance().getCurrentUser(new MRGSAuthentication.UserCallback() {
@Override
public void onSuccess(@NonNull final MRGSUser user) {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
// Returns authentication's information.
MRGSAmazon.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🔗
In order to "log out" of a user’s account, use the method:
Extra🔗
Note that if you requested additional permissions from the user to access additional fields of his profile, we will request them automatically, and the data will appear either in the corresponding fields of the object of the MRGSAuthenticationUser class, or in the optionalParams field of this object.
Created: 2020-05-28