Google デジタル アセット リンクのサンプル

多くの場合、サンプルコードは API の使い方を学ぶ最も簡単な方法です。Google Digital Asset Links のサンプルへのリンクについては、以下のプログラミング言語を選択してください。

サンプルでは、Google API クライアント ライブラリを使用しています。

図書館のサンプルページに そのライブラリは引き続き使用でき、 異なる Google API 用に提供されているサンプルを適応させます。

Python

次の簡単な Python の例は、特定のウェブサイトによって行われたすべてのステートメントを一覧表示し、そのサイトが特定の Android アプリについて delegate_permission/common.handle_all_urls ステートメントを作成しているかどうかを確認します。

#!/usr/bin/python

import urllib

def ListWeb(source_web_site, relation):
  return urllib.urlopen(
      'https://digitalassetlinks.googleapis.com/v1/'
      'statements:list?source.web.site=%s&relation=%s'
      % (urllib.quote(source_web_site, ''),
         urllib.quote(relation, ''))).read()

def CheckWebToAndroid(source_web_site, relation,
                      target_package_name, target_sha256_fingerprint):
  return urllib.urlopen(
      'https://digitalassetlinks.googleapis.com/v1/'
      'assetlinks:check?source.web.site=%s&relation=%s'
      '&target.android_app.package_name=%s'
      '&target.android_app.certificate.sha256_fingerprint=%s'
      '&key=API_KEY'
      % (urllib.quote(source_web_site, ''),
         urllib.quote(relation, ''),
         urllib.quote(target_package_name, ''),
         urllib.quote(target_sha256_fingerprint, ''))).read()

def main():
  print '================================== List() Output ======='
  print ListWeb('http://example.digitalassetlinks.org',
                'delegate_permission/common.handle_all_urls')
  print '================================== Check() Output ======'
  print CheckWebToAndroid(
      'http://example.digitalassetlinks.org',
      'delegate_permission/common.handle_all_urls',
      'org.digitalassetlinks.sampleapp',
      '10:39:38:EE:45:37:E5:9E:8E:E7:92:F6:54:50:4F:B8:34:6F:C6:B3:46:D0:BB:C4:41:5F:C3:39:FC:FC:8E:C1')

if __name__ == '__main__':
  main()

JavaScript

特定のウェブサイトが行ったすべてのステートメントを一覧表示し、そのウェブサイトに特定のステートメントが存在するかどうかを確認できる簡単な JavaScript の例を次に示します。

<html>
  <head>
    <script type="text/javascript">
      function executeRequest(request, outElement) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
              outElement.value = xmlhttp.responseText;
            } else {
              outElement.value = "Error running request. Response: "
                  + xmlhttp.responseText;
            }
          }
        };
        xmlhttp.open('GET', 'https://digitalassetlinks.googleapis.com/v1/' +
            request, true);
        xmlhttp.send();
      }

      function executeListRequest() {
        var sourceWebSite = encodeURIComponent(
            document.getElementById('list_source').value);
        var relation = encodeURIComponent(
            document.getElementById('list_relation').value);
        var outputTextArea = document.getElementById('list_response');
        executeRequest('statements:list?source.web.site=' + sourceWebSite
            + '&relation=' + relation, outputTextArea);
      }

      function executeCheckRequest() {
        var sourceWebSite = encodeURIComponent(
            document.getElementById('check_source').value);
        var relation = encodeURIComponent(
            document.getElementById('check_relation').value);
        var targetPackageName = encodeURIComponent(
            document.getElementById('check_target_package').value);
        var targetSha256Fingerprint = encodeURIComponent(
            document.getElementById('check_target_sha256_fingerprint').value);
        var outputTextArea = document.getElementById('check_response');
        executeRequest('assetlinks:check?source.web.site=' + sourceWebSite
            + '&relation=' + relation
            + '&target.android_app.package_name=' + targetPackageName
            + '&target.android_app.certificate.sha256_fingerprint='
            +     targetSha256Fingerprint
            + '&key=API_KEY',
            outputTextArea);
      }

    </script>
  </head>
  <body>
    <h2>List()</h2>
    <label>Source Web Asset:</label>
    <input type="text" id="list_source"
        value="http://example.digitalassetlinks.org">
     
    <label>Relation:</label>
    <input type="text" id="list_relation"
        value="delegate_permission/common.handle_all_urls">
     
    <button type="button" onclick="executeListRequest()">Run</button><br>
    <textarea rows="20" cols="80" id="list_response"></textarea>
    <hr>
    <h2>Check()</h2>
    <label>Source Web Asset:</label>
    <input type="text" id="check_source"
        value="http://example.digitalassetlinks.org">
     
    Relation:
    <input type="text" id="check_relation"
        value="delegate_permission/common.handle_all_urls"><br>
     
    <label>Target Android Package:</label>
    <input type="text" id="check_target_package"
        value="org.digitalassetlinks.sampleapp">
     
    <label>Target Android Certificate Fingerprint:</label>
    <input type="text" id="check_target_sha256_fingerprint"
        value="10:39:38:EE:45:37:E5:9E:8E:E7:92:F6:54:50:4F:B8:34:6F:C6:B3:46:D0:BB:C4:41:5F:C3:39:FC:FC:8E:C1">
     
    <button type="button" onclick="executeCheckRequest()">Run</button><br>
    <textarea rows="20" cols="80" id="check_response"></textarea>
  </body>
</html>

Go

このバージョンの Google デジタル アセット リンク専用の Go サンプルはありません。

ただし、他の Go サンプルのいずれかを適応できる場合があります。

Java

このバージョンの Google デジタル アセット リンク専用の Java サンプルはありません。

他のJava サンプルを適応できる場合があります。

.NET

このバージョンの Google デジタル アセット リンクに固有の .NET サンプルはありません。

ただし、他の .NET サンプルを適応できる場合があります。

Objective-C

このバージョンの Google デジタル アセット リンクに固有の Objective-C サンプルはありません。

ただし、他の Objective-C サンプルを適応させることもできます。

PHP

このバージョンの Google デジタル アセット リンクに固有の PHP サンプルはありません。

ただし、どちらか一方のアルゴリズムを適応させる PHP のサンプル

Ruby

このバージョンの Google デジタル アセット リンクに固有の Ruby サンプルはありません。

ただし、他の Ruby サンプルのいずれかを適応させることもできます。