Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
New Google Sign-In API
Search
syarihu
December 04, 2015
Technology
0
1.3k
New Google Sign-In API
Google Play Services 8.3で新しく出てきたGoogle Sign-In APIの話と、僕が以前作ったOAuthHelperの話です。
syarihu
December 04, 2015
Tweet
Share
More Decks by syarihu
See All by syarihu
Recap #io25 What's new in Google Play
syarihu
0
78
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
4
4.3k
[shibuya.apk #46] Composableの枠を超えてアニメーションする / Animation beyond Composable
syarihu
0
80
[shibuya.apk #41] Jetpack Composeでグリッドに柔軟にスペースを入れたい
syarihu
0
3.4k
[DroidKaigi 2022] 詳解Google Playの新しい定期購入 ~オファーの活用や実装例を添えて~
syarihu
0
3.9k
5分で分かるGoogle Playの新しいサブスクリプション / Google Play new subscription 2022
syarihu
1
1.9k
[DroidKaigi 2021] Google Play 定期購入 比例配分モード完全攻略ガイド / Google Play Subscription Proration Mode Complete Guide
syarihu
1
8k
[Money Forward Tech Drive] What's new in Google Play [Recap #io21]
syarihu
0
600
[Android 11 Meetups] Google Play Commerce からのアップデート / Android 11 Meetups Google Play Commerce
syarihu
4
2.9k
Other Decks in Technology
See All in Technology
AI駆動開発の実践とその未来
eltociear
1
440
通勤手当申請チェックエージェント開発のリアル
whisaiyo
3
310
20251222_サンフランシスコサバイバル術
ponponmikankan
2
120
「図面」から「法則」へ 〜メタ視点で読み解く現代のソフトウェアアーキテクチャ〜
scova0731
0
430
AWS re:Invent 2025 re:Cap LT大会 データベース好きが語る re:Invent 2025 データベースアップデート/セッションの紹介
coldairflow
0
130
ZOZOの独自性を生み出す「似合う4大要素」の開発サイクル
zozotech
PRO
0
100
Connection-based OAuthから学ぶOAuth for AI Agents
flatt_security
0
200
AgentCoreとStrandsで社内d払いナレッジボットを作った話
motojimayu
1
380
ウェルネス SaaS × AI、1,000万ユーザーを支える 業界特化 AI プロダクト開発への道のり
hacomono
PRO
0
310
Database イノベーショントークを振り返る/reinvent-2025-database-innovation-talk-recap
emiki
0
250
シニアソフトウェアエンジニアになるためには
kworkdev
PRO
3
210
【ServiceNow SNUG Meetup LT deck】WorkFlow Editorの廃止と Flow Designerへの移行戦略
niwato
0
110
Featured
See All Featured
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
200
Code Reviewing Like a Champion
maltzj
527
40k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.3k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
680
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
130
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.6k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
77
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
62
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
0
220
Transcript
New Google Sign-In API 2015/12/04 (Fri) shibuya.apk #5 @syarihu
GMO Media, Inc. Engineer - Android - Server Side Java
@syarihu (しゃりふ)
では、さっそく
New Google Sign-In API
の前に
以前のGoogle Sign-Inの話から…
Previous Google Sign-In... - アカウント選択画面 ( AccountManager.newChooseAccountIntent ) - 自分でIntent投げて、
返ってきたらごにょごにょやって… - AuthTokenの取得 ( AccountManager#getAuthToken ) - 認証状態の確認 - 認証されてなかったら認証許可ダイアログを表示 - 認証されてたらAuthTokenを取得 - 認証許可が外れてたら再取得も自分で…
Previous Google Sign-In... - アカウントの選択でつかうメソッド AccountManager.newChooseAccountIntent(Account selectedAccount, ArrayList<Account> allowableAccounts, String[]
allowableAccountTypes, boolean alwaysPromptForAccount, String descriptionOverrideText, String addAccountAuthTokenType, String[] addAccountRequiredFeatures, Bundle addAccountOptions) - AuthTokenの取得でつかうメソッド AccountManager#getAuthToken(Account account, String authTokenType, Bundle options, boolean notifyAuthFailure, AccountManagerCallBack<Bundle> callback, Handler handler)
(´゚д゚`)
なんだこれ。。
引数多い、みにくい、つらい。。
これ、実際に使おうと思うと結構たいへん。。
なので
OAuthHelperとかいうのを作ってみた
OAuthHelperとは - アカウント選択 - 認証許可確認 - AuthTokenの取得 - 再認証
OAuthHelperとは - アカウント選択 - 認証許可確認 - AuthTokenの取得 - 再認証 これらのめんどうな処理を
まとめてやってくれます
使い方は簡単
1. OAuthHelper.OnAuthListenerを実装 public class MainActivity extends ActionBarActivity implements OAuthHelper.OnAuthListener {
@Override public void getAuthToken(String authToken) { // ここにAuthTokenを使った処理を書く } }
2. OAuthHelperのインスタンスを生成 public class MainActivity extends ActionBarActivity implements OAuthHelper.OnAuthListener {
@Override protected void onCreate(Bundle savedInstanceState) { ... // Activity activity, String Scopes, OnAuthListener listener mHelper = new OAuthHelper( this, "oauth2:" + UrlshortenerScopes.URLSHORTENER, this ); ... } }
3. Intentから戻ってきた時の処理 ... @Override protected void onActivityResult(int requestCode, int resultCode,
Intent data) { super.onActivityResult(requestCode, resultCode, data); // アカウント選択や認証画面から返ってきた時の処理を OAuthHelperで受け取る mHelper.onActivityResult(requestCode, resultCode, data); } ...
4. 認証開始 mHelper.startAuth(false);
4. 認証開始 mHelper.startAuth(false); ↑ これの引数はAuthTokenの再取得するかどうか
実行するとこうなります
実行するとこうなります
実行するとこうなります
よし!
これでGoogle Sign-Inを実装するのが楽になったぞ!
ε=\_〇ノヒャッホーイ!!
が…
新しいGoogle Sign-In APIが出たので
作って半年で要らない子に。。
il||li_◦/ ̄|_il||li ナンテコッタィ
まぁそんな子は放っておいて
新しいGoogle Sign-In APIを使いましょう
New Google Sign-In API - アカウントを取得してサインインするのに権限が必要ない - 新しくデザインされたサインインボタン - Slient
Sign-In - 実装も以前より簡単
こんな感じの画面
こんな感じの画面
こんな感じの画面
こんな感じの画面
Google Sign-In APIの実装 - GoogleSignInOptionsオブジェクトを作成 - GoogleApiClientオブジェクトを作成 - Googleサインインボタンを追加する -
サインイン処理の実装
1. GoogleSignInOptionsオブジェクトを作成する // 許可させたいAPIのスコープ mScope = new Scope("https://www.googleapis.com/auth/urlshortener"); mGoogleSignInOptions =
new GoogleSignInOptions .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(mScope) .requestEmail() .build();
2. GoogleApiClientオブジェクトを作成する mGoogleApiClient = new GoogleApiClient.Builder(this) /* FragmentActivity, OnConnectionFailedListener */
.enableAutoManage(this, this) .addApi(Auth.GOOGLE_SIGN_IN_API, mGoogleSignInOptions) .addScope(mScope) .build();
3. Googleサインインボタンを追加する <com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
3. Googleサインインボタンを追加する SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); signInButton.setOnClickListener(this); // セットすると、スコープに適したボタンになる
signInButton.setScopes(mGoogleSignInOptions.getScopeArray()); signInButton.setSize(SignInButton.SIZE_WIDE);
ScopeがGoogle+だとこんな感じ 3. Googleサインインボタンを追加する
4. サインイン処理の実装 private void signIn() { // サインイン用のIntentを取得してIntentを投げる Intent signInIntent
= Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } @Override public void onClick(View v) { if(v.getId() == R.id.sign_in_button) { signIn(); } }
4. サインイン処理の実装 @Override public void onActivityResult(int requestCode, int resultCode, Intent
data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { // サインインした結果を取得 GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } }
4. サインイン処理の実装 private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handleSignInResult:" +
result.isSuccess()); if (result.isSuccess()) { // サインインが成功したら、サインインボタンを消して、ユーザー名を表示する mGoogleSignInAccount = result.getSignInAccount(); mStatusTextView.setText( "DisplayName: " + mGoogleSignInAccount.getDisplayName() ); } }
5. サインアウト処理の実装 private void signOut() { Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( new ResultCallback<Status>() {
@Override public void onResult(Status status) { showSnackBar(getString(R.string.signed_out)); } }); }
6. OAuthTokenの取得 accessToken = GoogleAuthUtil.getToken( MainActivity.this, new Account(mGoogleSignInAccount.getEmail(), "com.google"), "oauth2:"
+ API_SCOPE );
7. Silent Sign-In OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn (mGoogleApiClient); if (opr.isDone())
{ // サインイン成功時 GoogleSignInResult result = opr.get(); handleSignInResult(result); } else { opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { @Override public void onResult(GoogleSignInResult googleSignInResult) { // 未サインイン時 } }); }
試しにこれを使ってGoogle Url Shortenerで URLを短縮するサンプルを作ってみました
Google Sign-In API Sample https://youtu.be/_BpuYe8w4PI
新しいGoogle Sign-In APIを使えば
こんなに手軽に Google Sign-Inを実装できる!
すばらしい!
OAuthHelperってなんだよ!誰もしらねえよ!!
あんな誰も知らない子のことは忘れて
みなさんぜひ新しいGoogle Sign-In APIを使いましょう
サンプルコードとか - 【Androidアプリ開発】AndroidでのOAuth認証が簡単に実装できる OAuthHelperを作ってみた http://diary.syarihu.net/2015/05/androidandroidoauthoauthhelper.html - [GitHub] OAuthHelperSample https://github.com/syarihu/OAuthHelperSample -
[GitHub] GoogleSignInTest https://github.com/syarihu/GoogleSignInTest
参考 - Integrating Google Sign-In into Your Android App https://developers.google.com/identity/sign-in/android/sign-in
- Google Sign-In Quickstart https://github.com/googlesamples/google- services/tree/master/android/signin
おしまい