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

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

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

ライブラリのサンプルページに Google デジタル アセット リンクのサンプルがまだない場合でも、そのライブラリを使用して、別の Google API に提供されるサンプルを適応させることができます。

Python

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

#!/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 サンプルのいずれかを適応させることもできます。