このドキュメントでは、バックエンドサーバーを持つアプリで、承認コードを使用してユーザー情報とセッション情報を取得することに焦点を当てます。
このフローの内容:
tokenとrefresh_tokenを取得します。
承認コードは、バックエンドサーバーがtokenおよびrefresh_tokenを取得するためにnow.ggサーバーと交換するためのワンタイムコードです。
client_idを使用して承認コードを生成し、ユーザーにサインインするには、以下の実装を参照してください:
public static final String CODE = "code";
public static final String CLIENT_ID = "your client id";
public static final String ACCOUNT_TYPE = "now.gg";
public static final String HOST_URL = "hostUrl";
private void signIn() {
Account account = getNowggAccount();
if (account != null) {
Bundle bundle = new Bundle();
bundle.putString("client_id", CLIENT_ID);
String authTokenType = CODE;
AccountManager.get(getApplicationContext()).
getAuthToken(account, authTokenType, bundle, MainActivity.this, new OnTokenAcquired(), null);
}
else {
addNowggAccount();
}
}
private Account getNowggAccount() {
Account[] accounts = AccountManager.get(getApplicationContext()).getAccountsByType(ACCOUNT_TYPE);
if (accounts.length > 0) {
Log.d(TAG, "getNowggAccount: account found");
// currently only one now.gg account can be added in a system
return accounts[0];
}
return null;
}
private void addNowggAccount() {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("gg.now.accounts", "gg.now.accounts.AuthenticatorActivity"));
intent.setAction("IAP_ADD_ACCOUNT");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
※この処理はOnTokenAcquired()関数内でコールバックを生成します。
ユーザがサインインした後、OnTokenAcquiredコールバック関数で要求された承認コードを取得します。承認コードを取得するには、以下を参照してください:
private class OnTokenAcquired implements AccountManagerCallback {
@Override
public void run(AccountManagerFuture result) {
try {
Bundle bundle = result.getResult();
boolean success = bundle.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
if (success) {
final String code = bundle.getString(‘authorization_code’);
// You are required to send this code to your backend server and call the Generate Tokens API to receive refresh_token/token with this code on app backend server and wait for response from server.
}
else {
// get token failed
// error case, developer can show error or show other login mechanisms
Log.d(TAG, "run: get token failed " + bundle);
}
} catch (AuthenticatorException e) {
e.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
} catch (OperationCanceledException e) {
e.printStackTrace();
}
}
}
承認コードを取得したら、それをアプリのバックエンドサーバーに送信し、Generate Tokens APIを使用してこのコードをnow.ggサーバーと交換し、tokenとrefresh_tokenを取得します。
注意:
tokenは、now.gg API(ユーザー情報とセッション情報)を呼び出すために使用します。tokenの有効期限が切れた場合、新しいtokenを取得するためにrefresh_tokenを保存します。tokenを取得したら、それを使ってユーザー情報とセッション情報を取得します。
tokenの有効期限が切れている場合は、Generate Tokens APIを使用してrefresh_tokenを生成することができます。
ユーザーアカウントサービス
ユーザーアカウントサービス
ドキュメント改訂版 1.0