Integrating Google Accounts
This topic explains how to allow your users to sign up for and log in to your mobile app with their Google account. Just add a few parameters in the developer portal and a few lines of code in your mobile app and you will be social-ready!
Configuring Google integration
Creating a Google application
To get started, you first need to create a Google application and get your Google client ID and client secret. For the detailed instructions, see the Google official documentation.
After you create a Google application, follow the steps below to get your Google client ID and client secret. In the "Authorized JavaScript origins" and "Authorized redirect URI", please specify the value that corresponds to the server location you've selected (please put your AppID in ${your-app-id}).
- Select "APIs & Auth" > "APIs" and enable "Google+ API".
- Select "APIs & Auth" > "Credentials" and set the following information on "OAuth" > "Create new client ID".
- Application Type (Please specify "Web application" here)
- Authorized JavaScript origins
- United States: https://${your-app-id}.us.kiiapps.com
- Japan: https://${your-app-id}.jp.kiiapps.com
- Singapore: https://${your-app-id}.sg.kiiapps.com
- Europe: https://${your-app-id}.eu.kiiapps.com
- Authorized redirect URI
- United States: https://${your-app-id}.us.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
- Japan: https://${your-app-id}.jp.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
- Singapore: https://${your-app-id}.sg.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
- Europe: https://${your-app-id}.eu.kiiapps.com/api/apps/${your-app-id}/integration/webauth/callback
When the setting is done, you will get a Google client ID and client secret.
Configuring a Kii application
Take the following steps in the developer portal to configure your application by setting your Google client ID and client secret.
Click on the "Edit" button in your application console in the developer portal.
Click "SOCIAL NETWORKS".
Click "Google" to open the configuration screen for Google.
Set your Google client ID and client secret.
Then, add the following tag under the application tag in your AndroidManifest.xml
so as to enable the Activity that the SDK uses for Google authentication.
<activity
android:name="com.kii.cloud.storage.social.connector.KiiSocialNetworkConnectorLoginActivity"
android:label="@string/app_name" />
Logging in with a Google account
The following example illustrates how to authenticate a user by using a Google access token.
// Create a KiiSocialConnect instance.
KiiSocialConnect connect = Kii.socialConnect(KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR);
// Specify Google as the social network provider for logging in to Kii Cloud.
Bundle option = new Bundle();
option.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.GOOGLEPLUS);
// Set a Google access token.
option.putString(KiiSocialNetworkConnector.ACCESS_TOKEN, accessToken);
// Authenticate the user to Kii Cloud with the Google access token.
connect.logIn(null, option, new KiiSocialCallBack() {
@Override
public void onLoginCompleted(KiiSocialConnect.SocialNetwork network, KiiUser user, Exception exception) {
if (exception != null) {
// Handle the error.
return;
}
}
});
The basic steps are as follows.
Get a Google access token. For the detailed instructions, see the Google official documentation.
Create an instance of a social network connector with the
socialConnect()
method. The argument isKiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR
.Create a
Bundle
instance and specify the target social network.Provider.GOOGLEPLUS
is specified here.Set the Google access token to the bundle.
Authenticate the user with the
logIn()
method. If the specified account is new to Kii Cloud, the user is registered before the login. ThelogIn()
method is non-blocking and receives the processing result with the callback method.When an access token is used for the login, all the processing is done in the background with no login screen for Google. You do not need to implement additional processings that are necessary when using the login UI provided by the SDK. Specifically, you do not need to specify the first parameter
Activity
of thelogIn()
method nor add code to theonActivityResult()
method.
If the login is successful, the user is cached inside the SDK as the current user and you can get the user information with the getCurrentUser()
method. You can also retrieve the access token and related parameters with the getAccessTokenBundle()
method as follows:
// Get the token bundle for the social network provider.
KiiSocialConnect connect = Kii.socialConnect(SocialNetwork.SOCIALNETWORK_CONNECTOR);
Bundle b = connect.getAccessTokenBundle();
// Get the access token.
String accessToken = b.getString("oauth_token");
// Get the user ID provided by the social network provider.
String providerUserId = b.getString("provider_user_id");
// Check if a new Kii user was created when the logIn() method was executed.
boolean kiiNewUser = b.getBoolean("kii_new_user");
Linking a Kii account to a Google account
You can link existing Kii users to their Google accounts. Linked users can log in to Kii Cloud via the Google authentication.
Here is the sample code for linking a Kii account to a Google account.
// Assume that a user has logged in.
// Create a KiiSocialConnect instance.
KiiSocialConnect connect = Kii.socialConnect(KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR);
// Specify Google as the social network provider for logging in to Kii Cloud.
Bundle option = new Bundle();
option.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.GOOGLEPLUS);
// Set a Google access token.
option.putString(KiiSocialNetworkConnector.ACCESS_TOKEN, accessToken);
// Link the Google account with the currently logged-in Kii user.
connect.link(null, option, new KiiSocialCallBack() {
@Override
public void onLinkCompleted(KiiSocialConnect.SocialNetwork network, KiiUser user, Exception exception) {
if (exception != null) {
// Handle the error.
return;
}
}
});
The basic steps are as follows.
Create an instance of a social network connector with the
socialConnect()
method. The argument isKiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR
.Create a
Bundle
instance and specify the target social network.Provider.GOOGLEPLUS
is specified here.Set a Google access token to the bundle. For getting a Google access token, see the Google official documentation.
Link the currently logged-in user with the Google account with the
link()
method. This method is non-blocking and receives the processing result in the callback method.
Unlinking a Kii account from a Google account
Here is the sample code for unlinking a Kii account from a Google account.
// Assume that a user has logged in.
// Create a KiiSocialConnect instance.
KiiSocialConnect connect = Kii.socialConnect(KiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR);
// Specify Google as the social network provider for logging in to Kii Cloud.
Bundle option = new Bundle();
option.putParcelable(KiiSocialNetworkConnector.PROVIDER, KiiSocialNetworkConnector.Provider.GOOGLEPLUS);
// Unlink the Google account from the currently logged-in Kii user.
connect.unlink(null, option, new KiiSocialCallBack() {
@Override
public void onUnLinkCompleted(KiiSocialConnect.SocialNetwork network, KiiUser user, Exception exception) {
if (exception != null) {
// Handle the error.
return;
}
}
});
The basic steps are as follows.
Create an instance of a social network connector with the
socialConnect()
method. The argument isKiiSocialConnect.SocialNetwork.SOCIALNETWORK_CONNECTOR
.Create a
Bundle
instance and specify the target social network.Provider.GOOGLEPLUS
is specified here.Unlink the currently logged-in user from the Google account with the
unlink()
method. This method is non-blocking and receives the processing result in the callback method.