如果您的應用程式使用密碼專用 Smart Lock 與網站共用使用者資料庫,或者您的應用程式和網站使用聯合登入提供者 (例如 Google 登入),您可以將應用程式與網站建立關聯,讓使用者儲存憑證一次,並自動登入應用程式和網站。
如要將應用程式與網站建立關聯,請在您的網站上代管 Digital Asset Links JSON 檔案,並在應用程式的資訊清單中新增 Digital Asset Link 檔案的連結,以宣告關聯。
在網站上代管 Digital Asset Links 宣告,也能讓網站在搭載 Android 8.0 以上版本時,與應用程式共用自動填入資料。
必要條件
您網站的登入網域必須可透過 HTTPS 存取。
為應用程式和網站建立關聯
建立 Digital Asset Links JSON 檔案。
舉例來說,如要宣告網站
https://signin.example.com
和含有套件名稱com.example
的 Android 應用程式可以共用登入憑證,請建立名為assetlinks.json
的檔案,其中含有以下內容:[{ "relation": ["delegate_permission/common.get_login_creds"], "target": { "namespace": "web", "site": "https://signin.example.com" } }, { "relation": ["delegate_permission/common.get_login_creds"], "target": { "namespace": "android_app", "package_name": "com.example", "sha256_cert_fingerprints": [ "F2:52:4D:82:E7:1E:68:AF:8C:BC:EA:B0:A2:83:C8:FE:82:51:CF:63:09:6A:4C:64:AE:F4:43:27:20:40:D2:4B" ] } }]
relation
欄位是一或多個字串陣列,用來描述要宣告的關係。如要宣告應用程式和網站共用登入憑證,請指定delegate_permission/common.get_login_creds
字串。target
欄位是一種物件,用於指定宣告所適用的資產。下列欄位可用來識別網站:namespace
web
site
網站網址,格式為
https://domain[:optional_port]
,例如https://www.example.com
。domain 必須完整。使用 HTTPS 通訊埠 443 時,必須省略 optional_port。
site
指定目標只能是根網域:您無法限制應用程式與特定子目錄建立關聯。請勿在網址中加入路徑,例如結尾的斜線。系統不會將子網域視為相符項目:也就是說,如果將 domain 指定為
www.example.com
,網域www.counter.example.com
就不會與您的應用程式建立關聯。下列欄位可用來識別 Android 應用程式:
namespace
android_app
package_name
應用程式資訊清單中宣告的套件名稱。例如: com.example.android
sha256_cert_fingerprints
應用程式簽署憑證的 SHA256 指紋。您可以使用下列指令產生指紋: $ keytool -list -v -keystore my-release-key.keystore
詳情請參閱 Digital Asset Links 參考資料。
請在登入網域的以下位置代管 Digital Assets Link JSON 檔案:
https://domain[:optional_port]/.well-known/assetlinks.json
舉例來說,如果您的登入網域為
signin.example.com
,請在https://signin.example.com/.well-known/assetlinks.json
上代管 JSON 檔案。Digital Assets Link 檔案的 MIME 類型必須為 JSON。確認伺服器會在回應中傳送
Content-Type: application/json
標頭。確保主機允許 Google 擷取您的 Digital Asset Link 檔案。如果您有
robots.txt
檔案,必須允許 Googlebot 代理程式擷取/.well-known/assetlinks.json
。大多數網站會直接允許所有自動化代理程式擷取/.well-known/
路徑中的檔案,讓其他服務存取這些檔案內的中繼資料:User-agent: * Allow: /.well-known/
在 Android 應用程式中宣告關聯。
在
<application>
下的資訊清單檔案中新增以下程式碼:<meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
在
strings.xml
檔案中加入asset_statements
字串資源。asset_statements
字串是 JSON 物件,指定要載入的assetlinks.json
檔案。您必須逸出在字串中使用的所有格號和引號。例如:<string name="asset_statements" translatable="false"> [{ \"include\": \"https://signin.example.com/.well-known/assetlinks.json\" }] </string>
> GET /.well-known/assetlinks.json HTTP/1.1 > User-Agent: curl/7.35.0 > Host: signin.example.com < HTTP/1.1 200 OK < Content-Type: application/json
將應用程式發布到 Google Play 商店。因此,您必須在公開管道中發布這個版本,系統才能接收關聯資料。
(選用) 填寫並提交 密碼專用 Smart Lock 聯盟表單,表示您已完成這項程序。Google 會定期檢查透過表單提交的聯盟是否確實有效,如果遇到問題,也可能會與您聯絡。
驗證完成後,應用程式使用者就能在您的應用程式或網站上儲存憑證,並自動登入兩者。
範例:將多個應用程式與一個網站建立關聯
如要將多個應用程式與網站建立關聯,請在 Digital Assets Link 檔案中指定每個應用程式。舉例來說,如要將 com.example
和 com.example.pro
應用程式與 https://signin.example.com/
上的網站建立關聯,請在 https://signin.example.com/.well-known/assetlinks.json
代管的 JSON 檔案中指定兩個應用程式:
[{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://signin.example.com"
}
},{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "android_app",
"package_name": "com.example",
"sha256_cert_fingerprints": [
"F2:52:4D:82:E7:1E:68:AF:8C:BC:EA:B0:A2:83:C8:FE:82:51:CF:63:09:6A:4C:64:AE:F4:43:27:20:40:D2:4B"
]
}
},{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "android_app",
"package_name": "com.example.pro",
"sha256_cert_fingerprints": [
"F2:52:4D:82:E7:1E:68:AF:8C:BC:EA:B0:A2:83:C8:FE:82:51:CF:63:09:6A:4C:64:AE:F4:43:27:20:40:D2:4B"
]
}
}]
接著,在這兩個應用程式中宣告關聯:
在
<application>
下的資訊清單檔案中新增以下程式碼:<meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
在
strings.xml
檔案中加入以下字串資源:<string name="asset_statements" translatable="false"> [{ \"include\": \"https://signin.example.com/.well-known/assetlinks.json\" }] </string>
範例:連結應用程式與多個網站
如要將應用程式與多個網站建立關聯,請在 Digital Assets Link 檔案中指定每個網站,並在每個網站上代管檔案。舉例來說,如要將 com.example
和 com.example.pro
應用程式與 https://signin.example.com/
和 https://m.example.com/
上的網站建立關聯,請在 https://signin.example.com/.well-known/assetlinks.json
代管的 JSON 檔案中指定應用程式和兩個網站:
[{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://signin.example.com"
}
},{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "web",
"site": "https://m.example.com"
},
},{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "android_app",
"package_name": "com.example",
"sha256_cert_fingerprints": [
"F2:52:4D:82:E7:1E:68:AF:8C:BC:EA:B0:A2:83:C8:FE:82:51:CF:63:09:6A:4C:64:AE:F4:43:27:20:40:D2:4B"
]
}
},{
"relation": ["delegate_permission/common.get_login_creds"],
"target": {
"namespace": "android_app",
"package_name": "com.example.pro",
"sha256_cert_fingerprints": [
"F2:52:4D:82:E7:1E:68:AF:8C:BC:EA:B0:A2:83:C8:FE:82:51:CF:63:09:6A:4C:64:AE:F4:43:27:20:40:D2:4B"
]
}
}]
接著,在 https://m.example.com/.well-known/assetlinks.json
代管的 JSON 檔案中加入主要 Digital Asset Links 檔案:
[{
"include": "https://signin.example.com/.well-known/assetlinks.json"
}]
最後,請在這兩個應用程式中宣告關聯:
在
<application>
下的資訊清單檔案中新增以下程式碼:<meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
在
strings.xml
檔案中加入以下字串資源:<string name="asset_statements" translatable="false"> [{ \"include\": \"https://signin.example.com/.well-known/assetlinks.json\" }] </string>