LoginSignup
1
1

More than 3 years have passed since last update.

AppAuthを利用して、HMSなしでHUAWEI IDログインを実現する

Posted at

HUAWEI IDログインの実装の振り返り

以前、HUAWEI IDログインの実装について紹介しました。

しかし、この実装方法はHMSを必要とします。HMSが入っていない端末だと動作しません。

今回はHMSなしの実装方法を紹介したいと思います。

前準備

  1. AppGallery Connectでプロジェクトとアプリを登録します。登録後に、APP IDが発行されます。
  2. AppAuthをダウンロードします。
git clone https://github.com/openid/AppAuth-Android.git

実装

APP IDが123456789と仮定します。
AppAuthの/res/raw/auth_config.jsonを次のように修正します。

auth_config.json
{
  "client_id": "123456789",
  "redirect_uri": "com.huawei.apps.123456789:/oauth2redirect",
  "end_session_redirect_uri": "net.openid.appauthdemo:/oauth2redirect",
  "authorization_scope": "openid email profile",
  "discovery_uri": "https://oauth-login.cloud.huawei.com/.well-known/openid-configuration",
  "authorization_endpoint_uri": "https://oauth-login.cloud.huawei.com/oauth2/v3/authorize",
  "token_endpoint_uri": "https://oauth-login.cloud.huawei.com/oauth2/v3/token",
  "registration_endpoint_uri": "",
  "user_info_endpoint_uri": "",
  "https_required": true
}

AppAuth-Androidモジュールのbuild.gradleを次のように修正します。

buidle.gradle
...
manifestPlaceholders = [
    'appAuthRedirectScheme': 'com.huawei.apps.123456789'
]
...

あとはAppAuthをそのまま利用すればよいです。

ユーザーのプロフィール

ログインしたら、IDトークンがAuthorizationService.TokenResponseCallback.onTokenRequestCompleted()でTokenResponseとして返されます。IDトークンがTokenResponse.idTokenにあります。IDトークンはJWTなので、JWTDecode.Androidなどのライブラリを使ってデコードすれば、ユーザーのプロフィールが取れます。

デコードしたIDトークンは次のようなフォーマットになります。

{
  ...
  "aud": "APP ID",
  "sub": "ユーザーID",
  "azp": "APP ID",
  "display_name": "ユーザー名",
  "email": "ユーザーのメールアドレス",
  "picture": "ユーザーの顔写真のリンク"
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1