本參考資料說明您將用來在網路應用程式中實作 Google 登入的 JavaScript 用戶端方法和屬性。
如果您在使用程式庫時遇到任何問題,請向我們的 GitHub 存放區回報。
驗證設定
載入 Google API 平台程式庫以建立 gapi
物件:
<script src="https://apis.google.com/js/platform.js?onload=init" async defer></script>
平台程式庫載入後,請載入 auth2
程式庫:
function init() {
gapi.load('auth2', function() {
/* Ready. Make a call to gapi.auth2.init or some other API */
});
}
gapi.auth2.init(params)
初始化 GoogleAuth
物件。您必須先呼叫這個方法,才能呼叫 gapi.auth2.GoogleAuth
的方法。
初始化 GoogleAuth
物件時,請使用 OAuth 2.0 用戶端 ID 設定物件,並設定任何其他要指定的選項。然後,如果使用者已經登入,GoogleAuth
物件就會還原使用者在上一個工作階段中的登入狀態。
引數 | |
---|---|
params |
包含用戶端設定資料鍵/值組合的物件。如要瞭解其他可設定屬性的資訊,請參閱 gapi.auth2.ClientConfig 。例如: { client_id: 'CLIENT_ID.apps.googleusercontent.com' } |
傳回 | |
---|---|
gapi.auth2.GoogleAuth |
gapi.auth2.GoogleAuth 物件。使用 then() 方法取得 gapi.auth2.GoogleAuth 物件完成初始化後解析的 Promise。
|
GoogleAuth.then(onInit, onError)
在 GoogleAuth
物件完全初始化時呼叫 onInit 函式。如果在初始化期間發生錯誤 (可能在舊版不支援的瀏覽器中發生),系統會改為呼叫 onError 函式。
引數 | |
---|---|
onInit |
程序完全初始化後,使用 GoogleAuth 物件呼叫的函式。 |
onError |
如果 GoogleAuth 無法初始化,則會使用包含 error 屬性的物件呼叫函式。 |
傳回 | |
---|---|
Promise | 在 onInit 函式完成時執行的 Promise ,如果引發初始化錯誤,則會遭到拒絕。這個函式會使用 onInit 函式傳回的值 (如果有的話) 進行解析。 |
錯誤代碼
idpiframe_initialization_failed
-
無法從 Google 初始化必要的 iframe,例如環境不受支援。
details
屬性可針對引發的錯誤提供更多資訊。
gapi.auth2.ClientConfig
代表 gapi.auth2.init
方法不同設定參數的介面。
參數 | ||
---|---|---|
client_id |
string |
必要。應用程式的用戶端 ID (可在 Google API 控制台中找到) 並建立。 |
cookie_policy |
string |
要建立登入 Cookie 的網域。URI、single_host_origin 或 none 。如未指定,預設為 single_host_origin 。 |
scope |
string |
要要求的範圍,以空格分隔字串。如果 fetch_basic_profile 未設為 false,則為選用屬性。 |
fetch_basic_profile |
boolean |
在使用者登入時擷取基本個人資料。將「profile」、「email」和「openid」新增至要求的範圍。如未指定,則為「true」。 |
hosted_domain |
string |
使用者必須登入的 G Suite 網域。用戶端容易修改,因此請務必確認所傳回使用者的代管網域資源。在用戶端上使用 GoogleUser.getHostedDomain(),並在伺服器上使用 ID 權杖中的 hd 憑證附加資訊,驗證網域是否符合預期。
|
use_fedcm |
boolean |
選用,預設為 True 。在登入時啟用或停用瀏覽器 FedCM API。 |
ux_mode |
string |
登入流程使用的使用者體驗模式。根據預設,系統預設會在彈出式視窗中開啟同意聲明流程。有效值為 popup 和 redirect 。 |
redirect_uri |
string |
如果使用 ux_mode='redirect' ,這個參數可讓您覆寫同意聲明流程結束時使用的預設 redirect_uri 。目前網址去除查詢參數和雜湊片段後,預設值為 redirect_uri 。 |
enable_granular_consent |
boolean |
選用設定。是否要啟用精細權限。如果設為「false 」,針對 2019 年前建立的 OAuth 用戶端 ID,系統會停用更精細的 Google 帳戶權限。2019 年期間或之後建立的 OAuth 用戶端 ID 不會受到任何影響,因為系統一律會為這類 ID 啟用更精細的權限。 |
plugin_name |
string |
選用設定。這個值設定完成後,2022 年 7 月 29 日前建立的新用戶端 ID 就能使用舊版 Google 平台資料庫。
根據預設,新建立的用戶端 ID 現已禁止使用平台程式庫,必須改用新版 Google Identity 服務程式庫。您可以選擇任何值,建議使用描述性名稱 (例如產品或外掛程式名稱) 以便識別。
範例:plugin_name: 'YOUR_STRING_HERE' |
驗證機制
GoogleAuth
是一種單例模式,它提供的方法可讓使用者以 Google 帳戶登入、取得使用者目前的登入狀態、取得使用者 Google 個人資料的特定資料、要求其他範圍,以及從目前帳戶登出。
gapi.auth2.getAuthInstance()
傳回 GoogleAuth
物件。您必須先使用 gapi.auth2.init()
初始化 GoogleAuth
物件,才能呼叫此方法。
傳回 | |
---|---|
gapi.auth2.GoogleAuth |
gapi.auth2.GoogleAuth 物件。請使用這個物件呼叫 gapi.auth2.GoogleAuth 的方法。 |
GoogleAuth.isSignedIn.get()
傳回目前使用者是否登入。
傳回 | |
---|---|
布林值 |
true :如果使用者已登入,或 GoogleAuth 物件尚未初始化,則為 false 。 |
GoogleAuth.isSignedIn.listen(listener)
監聽目前使用者登入狀態的變更。
引數 | |
---|---|
listener |
接受布林值的函式。在使用者登入時,listen() 會將 true 傳遞至這個函式,並在使用者登出時傳遞 false 。 |
GoogleAuth.signIn()
透過指定 gapi.auth2.init()
的選項登入使用者。
傳回 | |
---|---|
Promise | 當使用者成功驗證並授權要求的範圍時,針對 GoogleUser 例項執行的 Promise ;如果發生錯誤,則會以包含 error 屬性的物件拒絕。如要瞭解錯誤代碼,請參閱下一節。 |
錯誤代碼
查看《GoogleAuth.signIn(options)
》。
GoogleAuth.signIn(options)
透過指定選項登入使用者。
引數 | |
---|---|
options |
以下兩者之一:
|
傳回 | |
---|---|
Promise | Promise ,當使用者成功驗證並授予要求的範圍,或在發生錯誤時因含有 error 屬性的物件遭拒 (請參閱下文瞭解錯誤代碼) 時,會以 GoogleUser 執行個體執行。 |
錯誤代碼
popup_closed_by_user
- 使用者在完成登入流程前關閉彈出式視窗。
access_denied
- 使用者拒絕了必要範圍的權限。
immediate_failed
-
無法在未提示同意聲明流程的情況下自動選取任何使用者。搭配使用
signIn
與prompt: 'none'
選項時引發錯誤。您不應使用這個選項,因為如果先前已在上一個工作階段中登入,gapi.auth2.init
會自動為使用者登入。
gapi.auth2.SignInOptions
代表 GoogleAuth.signIn(options)
方法不同設定參數的介面。
參數 | ||
---|---|---|
prompt |
string |
針對同意聲明流程強制執行特定模式。選用。 可能的值包括:
|
scope |
string |
要要求的範圍 (以空格分隔的字串),位於 gapi.auth2.init 參數中定義的範圍。如果 fetch_basic_profile 未設為 false,則為選用元素。
|
ux_mode |
string |
登入流程使用的使用者體驗模式。根據預設,系統預設會在彈出式視窗中開啟同意聲明流程。有效值為 popup 和 redirect 。 |
redirect_uri |
string |
如果使用 ux_mode='redirect' ,這個參數可讓您覆寫同意聲明流程結束時使用的預設 redirect_uri 。目前未移除查詢參數和雜湊片段的網址預設的 redirect_uri 。 |
GoogleAuth.signOut()
從應用程式登出目前的帳戶。
傳回 | |
---|---|
Promise | 在使用者登出後執行的 Promise 。 |
GoogleAuth.disconnect()
撤銷使用者授予的所有範圍。
GoogleAuth.grantOfflineAccess(options)
請取得使用者授權,以便離線存取指定範圍。
引數 | |
---|---|
options |
包含參數鍵/值組合的 gapi.auth2.OfflineAccessOptions 物件。例如: { scope: 'profile email' } |
傳回 | |
---|---|
Promise | 在使用者授予要求的範圍時執行的 Promise ,並將包含授權碼的物件傳遞至 Promise 的執行要求處理常式。例如: auth2.grantOfflineAccess().then(function(resp) { var auth_code = resp.code; }); |
錯誤代碼
popup_closed_by_user
- 使用者在完成同意聲明流程前關閉彈出式視窗。
access_denied
- 使用者拒絕了必要範圍的權限。
immediate_failed
-
無法在未提示同意聲明流程的情況下自動選取任何使用者。搭配使用
signIn
與prompt: 'none'
選項時引發錯誤。您不應使用這個選項,因為如果先前已在上一個工作階段中登入,gapi.auth2.init
會自動將使用者登入。
gapi.auth2.OfflineAccessOptions
代表 GoogleAuth.grantOfflineAccess(options)
方法不同設定參數的介面。
參數 | ||
---|---|---|
prompt |
string |
針對同意聲明流程強制執行特定模式。選用。 可能的值包括:
|
scope |
string |
要要求的範圍 (以空格分隔的字串),位於 gapi.auth2.init 參數中定義的範圍。如果 fetch_basic_profile 未設為 false,則為選用元素。
|
GoogleAuth.attachClickHandler(container, options, onsuccess, onfailure)
將登入流程附加至指定容器的點擊處理常式。
引數 | |
---|---|
container | 附加點擊處理常式的 div 元素 ID 或參照。 |
options | 包含參數鍵/值組合的物件。請參閱 GoogleAuth.signIn()。 |
onsuccess | 登入完成後要呼叫的函式。 |
onfailure | 登入失敗時要呼叫的函式。 |
使用者
GoogleUser
物件代表一個使用者帳戶。GoogleUser
物件通常是透過呼叫 GoogleAuth.currentUser.get() 取得。
GoogleAuth.currentUser.get()
傳回代表目前使用者的 GoogleUser
物件。請注意,在剛初始化的 GoogleAuth
例項中,尚未設定目前的使用者。使用 currentUser.listen()
方法或 GoogleAuth.then()
取得初始化的 GoogleAuth
例項。
傳回 | |
---|---|
GoogleUser |
目前的使用者 |
GoogleAuth.currentUser.listen(listener)
監聽 currentUser 的變更。
引數 | |
---|---|
listener |
採用 GoogleUser 參數的函式。每次修改 currentUser 時,listen 都會向此函式傳遞 GoogleUser 例項。 |
GoogleUser.getId()
取得使用者的專屬 ID 字串。
傳回 | |
---|---|
字串 | 使用者的專屬 ID |
GoogleUser.isSignedIn()
如果使用者已登入,則傳回 true。
傳回 | |
---|---|
布林值 | 如果使用者已登入,則為「是」 |
GoogleUser.getHostedDomain()
如果使用者已透過 G Suite 帳戶登入,請取得使用者的 G Suite 網域。
傳回 | |
---|---|
字串 | 使用者的 G Suite 網域 |
GoogleUser.getGrantedScopes()
取得使用者授予以空格分隔字串的範圍。
傳回 | |
---|---|
字串 | 使用者授予的範圍 |
GoogleUser.getBasicProfile()
取得使用者的基本個人資料。
傳回 | |
---|---|
gapi.auth2.BasicProfile |
您可以使用下列方法擷取 gapi.auth2.BasicProfile 的屬性:
|
GoogleUser.getAuthResponse(includeAuthorizationData)
從使用者驗證工作階段取得回應物件。
引數 | |
---|---|
includeAuthorizationData | 選用:這個布林值可指定是否一律傳回存取權杖和範圍。根據預設,當 fetch_basic_profile 為 true (預設值) 且未要求其他範圍時,系統不會傳回存取權杖和要求的範圍。 |
傳回 | |
---|---|
gapi.auth2.AuthResponse |
gapi.auth2.AuthResponse 物件。 |
GoogleUser.reloadAuthResponse()
強制重新整理存取權杖,然後傳回新 AuthResponse 的 Promise。
傳回 | |
---|---|
Promise |
重新載入 OAuth 權杖時,透過重新載入的 gapi.auth2.AuthResponse 執行的 Promise 。 |
gapi.auth2.AuthResponse
呼叫 GoogleUser.getAuthResponse(includeAuthorizationData)
或 GoogleUser.reloadAuthResponse()
方法時傳回的回應。
屬性 | ||
---|---|---|
access_token |
string |
已授予存取權杖。 |
id_token |
string |
授予的 ID 權杖。 |
scope |
string |
存取權杖中授予的範圍。 |
expires_in |
number |
存取 Token 到期前的秒數。 |
first_issued_at |
number |
使用者首次授予要求範圍時的時間戳記。 |
expires_at |
number |
存取權杖效期的時間戳記。 |
GoogleUser.hasGrantedScopes(scopes)
如果使用者授予指定範圍,則傳回 true。
引數 | |
---|---|
scopes | 以空格分隔的範圍字串。 |
傳回 | |
---|---|
布林值 | 如果範圍已授予,則為 True |
GoogleUser.grant(options)
向使用者要求其他範圍。
如需參數清單和錯誤代碼,請參閱 GoogleAuth.signIn()
。
GoogleUser.grantOfflineAccess(options)
請取得使用者授權,以便離線存取指定範圍。
引數 | |
---|---|
options |
包含參數鍵/值組合的 gapi.auth2.OfflineAccessOptions 物件。例如: { scope: 'profile email' } |
GoogleUser.disconnect()
撤銷使用者為應用程式授予的所有範圍。
UI 元素
gapi.signin2.render(id, options)
使用 options 物件指定的設定,在含有指定 ID 的元素中顯示登入按鈕。
引數 | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id | 顯示登入按鈕的元素 ID。 | ||||||||||||||||
options |
物件,包含用於呈現按鈕的設定。例如: { scope: 'email', width: 200, height: 50, longtitle: true, theme: 'dark', onsuccess: handleSuccess, onfailure: handleFailure }您可以指定下列選項:
|
進階
gapi.auth2.authorize(params, callback)
執行一次性 OAuth 2.0 授權。視所使用的參數而定,此設定會開啟 Google 登入流程的彈出式視窗,或嘗試在使用者沒有互動的情況下,自動載入要求的回應。
這種方法的適用情況包括:
- 您的應用程式只需要要求一次 Google API 端點,例如在使用者首次登入時載入喜愛的 YouTube 影片。
- 您的應用程式有自己的工作階段管理基礎架構,且只需要一次 ID 權杖即可在後端識別使用者。
- 同一個網頁中使用了多個用戶端 ID。
引數 | |
---|---|
params |
包含設定資料鍵/值組合的物件。如要瞭解其他可設定屬性的資訊,請參閱 gapi.auth2.AuthorizeConfig 。例如: { client_id: 'CLIENT_ID.apps.googleusercontent.com', scope: 'email profile openid', response_type: 'id_token permission' } |
callback |
要求完成 (成功或失敗) 後,使用 gapi.auth2.AuthorizeResponse 物件呼叫的函式。 |
範例
gapi.auth2.authorize({
client_id: 'CLIENT_ID.apps.googleusercontent.com',
scope: 'email profile openid',
response_type: 'id_token permission'
}, function(response) {
if (response.error) {
// An error happened!
return;
}
// The user authorized the application for the scopes requested.
var accessToken = response.access_token;
var idToken = response.id_token;
// You can also now use gapi.client to perform authenticated requests.
});
錯誤代碼
idpiframe_initialization_failed
-
因為不支援的環境,無法從 Google 初始化必要的 iframe。
details
屬性可針對引發的錯誤提供更多資訊。 popup_closed_by_user
- 使用者在完成登入流程前關閉彈出式視窗。
access_denied
- 使用者拒絕了必要範圍的權限。
immediate_failed
-
無法在未提示同意聲明流程的情況下自動選取任何使用者。搭配使用
signIn
與prompt: 'none'
選項時引發錯誤。
gapi.auth2.AuthorizeConfig
代表 gapi.auth2.authorize
方法不同設定參數的介面。
屬性 | ||
---|---|---|
client_id |
string |
必填。應用程式的用戶端 ID (可在 Google API 控制台中找到) 並建立。 |
scope |
string |
必填。要要求的範圍,以空格分隔字串。 |
response_type |
string |
以空格分隔的回應類型清單。預設為 'permission' 。可能的值包括:
|
prompt |
string |
針對同意聲明流程強制執行特定模式。可能的值包括:
|
cookie_policy |
string |
要建立登入 Cookie 的網域。URI、single_host_origin 或 none 。如未指定,預設值為 single_host_origin 。 |
hosted_domain |
string |
使用者必須登入的 G Suite 網域。用戶端可能會修改此情況,因此請務必驗證所傳回使用者的代管網域資源。 |
login_hint |
string |
在登入流程中預先選取的使用者電子郵件或使用者 ID。除非使用 prompt: "none" ,否則這種情況可能會遭到使用者修改。 |
include_granted_scopes |
boolean |
要求的存取權杖是否包含使用者先前授予應用程式的所有範圍,或是只要求目前呼叫中要求的範圍。這個變數預設為 true 。
|
enable_granular_consent |
boolean |
選用設定。是否要啟用精細權限。如果設為「false 」,針對 2019 年前建立的 OAuth 用戶端 ID,系統會停用更精細的 Google 帳戶權限。2019 年期間或之後建立的 OAuth 用戶端 ID 不會受到任何影響,因為系統一律會為這類 ID 啟用更精細的權限。 |
plugin_name |
string |
選用設定。設定後,2022 年 7 月 29 日前建立的用戶端 ID 即可使用 Google 平台資料庫。根據預設,新建的用戶端 ID 會禁止使用平台程式庫,因此必須改用新版 Google Identity 服務程式庫。您可以選擇任何值,建議使用描述性名稱 (例如產品或外掛程式名稱),以便識別。
範例:plugin_name: 'YOUR_STRING_HERE' |
gapi.auth2.AuthorizeResponse
傳回 gapi.auth2.authorize
方法回呼的回應。
屬性 | ||
---|---|---|
access_token |
string |
已授予存取權杖。只有在 response_type 中指定了 permission 或 token 時才能使用。 |
id_token |
string |
授予 ID 權杖。只有在 response_type 中指定了 id_token 時才能使用。 |
code |
string |
已授予授權碼。只有在 response_type 中指定了 code 時才能使用。 |
scope |
string |
存取權杖中授予的範圍。只有在 response_type 中指定了 permission 或 token 時才能使用。 |
expires_in |
number |
存取 Token 到期之前的秒數。只有在 response_type 中指定了 permission 或 token 時才能使用。 |
first_issued_at |
number |
使用者首次授予要求範圍時的時間戳記。只有在 response_type 中指定了 permission 或 token 時才能使用。 |
expires_at |
number |
存取權杖效期的時間戳記。只有在 response_type 中指定了 permission 或 token 時才能使用。 |
error |
string |
要求失敗時,這個指令會包含錯誤代碼。 |
error_subtype |
string |
要求失敗時,這可能包含一併傳回的錯誤代碼額外資訊。 |