-
アプリのstrings.xmlに次の文字列を追加します:
<string name='ui.label.appname' translatable='false'> YOUR APP NAME </string>
<string name='iam_portal_url'>
https://accounts.zohoportal.com</string><!-- 利用しているデータセンターのアカウントURLを入力します。例:EUのユーザーは次のURLを使用します:
https://accounts.zohoportal.eu-->
<string name='c_id'> GENERATED CLIENT ID </string>
<string name='c_secret'> GENERATED CLIENT SECRET ID </string>
<string name='c_portal'> PORTAL ID </string>
<string name='redir_url'> REGISTERED REDIRECTURI</string> <!--これらのパラメーターは、手順2に記載されているURLから取得します-->
-
アプリ/アクティビティクラスのoncreateメソッドで、次のようにAccounts SDKを初期化します:
IAMClientSDK.getInstance(this).init(scopes)
scopesには次の値を指定します:'aaaserver.profile.READ,zohocontacts.userphoto.READ,ZohoContacts.contactapi.READ,
ZohoCreator.meta.CREATE,ZohoCreator.meta.READ,ZohoCreator.meta.UPDATE,
ZohoCreator.meta.DELETE,ZohoCreator.data.CREATE,ZohoCreator.data.READ,
ZohoCreator.data.UPDATE,ZohoCreator.data.DELETE,Stratus.stratusop.READ,
ZohoCRM.modules.READ,ZohoCRM.users.READ'
メモ:バージョン1.4以降では、スコープの一覧が更新されています。
-
HandleRedirectActivityという名前のアクティビティを作成し、そのoncreateメソッドに次のコードを追加します:
IAMClientSDK.getInstance(this).handleRedirection(this)
-
AndroidManifest.xmlファイルに次のコードを追加します:
<activity android:name='.HandleRedirectActivity' android:exported='true'>
<intent-filter>
<action android:name='android.intent.action.VIEW' />
<category android:name='android.intent.category.DEFAULT' />
<category android:name='android.intent.category.BROWSABLE' />
<data android:scheme='<REDIRECTURI_WITHOUT_COLON_AND_SLASH>' />
</intent-filter>
</activity>
-
ログイン画面を開くには、次のコードスニペットを追加します:
クライアントの作成時に[JWTトークンを生成]を有効にした場合は、[JWTトークンあり]タブのコードスニペットを追加します。それ以外の場合は、[JWTトークンなし]タブのコードを追加します。
JWTトークンなし
val sdk = IAMClientSDK.getInstance(applicationContext)
if (!sdk.isUserSignedIn) {
sdk.presentLoginScreen(this, object : IAMTokenCallback {
override fun onTokenFetchInitiated() {
}
override fun onTokenFetchComplete(zohoToken: IAMToken) {
//<This method will be called after user logged-in successfully>
}
override fun onTokenFetchFailed(zohoErrorCodes: IAMErrorCodes) {
//<This method will be called if any error occurred in the login. You will receive the error code in this callback.>
}
}, null)
} else {
// start intent to your first activity
}
JWTトークンあり
val sdk = IAMClientSDK.getInstance(applicationContext)
if (!sdk.isUserSignedIn) {
IAMClientSDK.getInstance(context).getRemoteTokenWithJWTToken(jwtToken, <PORTAL ID>, object : IAMTokenCallback {
override fun onTokenFetchInitiated() {
}
override fun onTokenFetchComplete(token: IAMToken) {
}
override fun onTokenFetchFailed(errorCode: IAMErrorCodes) {
if (errorCode == IAMErrorCodes.user_cancelled) {
return
}
//This method will be called if any error occurred in the login. You will receive the error code in this callback.
}
})
} else {
// start intent to your first activity
}
-
次のように、アプリにCreatorの認証インターフェースZCOauthHelperを実装します:
class ZCAuthImpl(private val context: Context) : ZCOauthHelper {
@Throws(ZCException::class)
override suspend fun getAccessToken(): String? {
return suspendCoroutine { continuation ->
IAMClientSDK.getInstance(context).getToken(object : IAMTokenCallback {
override fun onTokenFetchInitiated() {
}
override fun onTokenFetchComplete(token: IAMToken) {
continuation.resumeWith(Result.success(token.token))
}
override fun onTokenFetchFailed(errorCode: IAMErrorCodes) {
continuation.resumeWith(Result.success(null))
}
})
}
}
override fun getInitialScopes(context: Context): String {
return ''
}
override fun isUserSignedIn(): Boolean {
return IAMClientSDK.getInstance(context).isUserSignedIn
}
override fun getUserData(): Any? {
return null
}
override fun getTransformedUrl(url: String): String {
return url
}
override fun checkAndLogout(): Boolean {
return false
}
override fun isEnhanceTokenNeeded(newScopes: String): Boolean {
return false
}
override fun enhanceToken(tokenHelper: ZCOauthHelper.ZCOAuthTokenHelper, newScopes: String) {
}
override fun enhanceTokenWithOnDemandScope(tokenHelper: ZCOauthHelper.ZCOAuthTokenHelper) {
}
}
-
認証インターフェースのインスタンスをCreator SDKに設定するため、アプリ/アクティビティのoncreateメソッドに次のコードスニペットを追加します:
ZCAPIUtil.setOAuthHelper(ZCAuthImpl(applicationContext))
-
AndroidManifest.xmlファイルのapplicationタグ内に次の内容を追加します:
android:name='com.zoho.creator.a.ZCreatorApplication'